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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用