douhu4692 2015-07-10 09:57 采纳率: 100%
浏览 180
已采纳

net / http:http:ContentLength = 222,正文长度为0

I'm trying to retry a request if there is a connection/proxy error. For some reasons I keep getting this error which doesn't seem to recover regardless the attepts to retry the request:

    Post https://m.somewebsite.co.uk/api/di/34433: http: ContentLength=222  with Body length 0

Am I doing something wrong? My first suspicion is that the http.Request is consumed somehow so on the next attempts it's no longer good. Should I manage a copy?

func Post(URL string, form url.Values, cl *http.Client) ([]byte, error) {
    req, err := http.NewRequest("POST", URL, strings.NewReader(form.Encode()))
    if err != nil {
        log.Error(err)
        return nil, err
    }
    req.Header.Set("User-Agent", ua)
    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    rsp, err := do(cl, req)

    if err != nil {
        return nil, err
    }
    defer rsp.Body.Close()
    b, err := ioutil.ReadAll(rsp.Body)
    if err != nil {
        log.Error(err)
        return nil, err
    }

    return b, nil
}

func do(cl *http.Client, req *http.Request)(*http.Response, error){
    rsp, err := cl.Do(req)
    for i := 0; IsErrProxy(err); i++ {
        log.Errorf("Proxy is slow or down ")
        time.Sleep(6 * time.Second)
5t      rsp, err = cl.Do(&ncp)
        if err == nil{
            return rsp, nil
        }
        if i > 10 {

            return nil, fmt.Errorf("after %v tries error: %v", i, err)
        }
    }
    return rsp, err
}
  • 写回答

3条回答 默认 最新

  • dongsi7759 2015-07-10 10:25
    关注

    The problem is that the request body is read to the end on the first call to Do(). On subsequent calls to Do(), no data is read from the response body.

    The fix is to move the creation of the body reader inside the for loop. This requires that the request also be created inside the for loop.

    func Post(URL string, form url.Values, cl *http.Client) ([]byte, error) {
      body := form.Encode()
      for i := 0; i < 10; i++ {
        req, err := http.NewRequest("POST", URL, strings.NewReader(body))
        if err != nil {
            log.Error(err)
            return nil, err
        }
        req.Header.Set("User-Agent", ua)
        req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
        rsp, err := cl.Do(req)
        if err == nil {
            defer rsp.Body.Close()
            b, err := ioutil.ReadAll(rsp.Body)
            if err != nil {
                log.Error(err)
                return nil, err
            }
            return b, nil
        }
    
        if !IsErrorProxy(err) {
            return nil, err
        }
    
        log.Errorf("Proxy is slow or down ")
        time.Sleep(6 * time.Second)
      }
      return nil, fmt.Errorf("after 10 tries error: %v", err)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100