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 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题