net / http:http:ContentLength = 222,主体长度为0 [英] net/http: http: ContentLength=222 with Body length 0

查看:254
本文介绍了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



我做错了什么?我的第一个怀疑是,http.Request是以某种方式消耗的,所以在接下来的尝试中它不再好。我应该管理一个副本吗?

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
}


推荐答案

问题在于请求在第一次调用Do()时,主体读到最后。在后续对Do()的调用中,没有数据会从响应主体中读取。

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.

解决方法是将身体读取器的创建移动到for循环中。这需要在for循环中创建请求。

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)
}

这篇关于net / http:http:ContentLength = 222,主体长度为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆