net/http: http: ContentLength=222,但Body长度为0。

huangapple go评论90阅读模式
英文:

net/http: http: ContentLength=222 with Body length 0

问题

我正在尝试在连接/代理错误时重新尝试请求。由于某些原因,我一直收到这个错误,无论重试请求多少次都无法恢复:

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

我做错了什么吗?我最初怀疑是http.Request在某种程度上被消耗了,所以在下一次尝试时它不再有效。我应该管理一个副本吗?

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

以上是你提供的代码。

英文:

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
}

答案1

得分: 26

问题在于第一次调用Do()时,请求体被完全读取。在后续调用Do()时,不会从响应体中读取任何数据。

修复方法是将创建请求体读取器的代码移到for循环内部。这要求请求也在for循环内部创建。

func Post(URL string, form url.Values, cl *http.Client) ([]byte, error) {
    for i := 0; i < 10; i++ {
        body := form.Encode()
        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)
}
英文:

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 &lt; 10; i++ {
	req, err := http.NewRequest(&quot;POST&quot;, URL, strings.NewReader(body))
	if err != nil {
		log.Error(err)
		return nil, err
	}
	req.Header.Set(&quot;User-Agent&quot;, ua)
	req.Header.Set(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;)
	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(&quot;Proxy is slow or down &quot;)
	time.Sleep(6 * time.Second)
  }
  return nil, fmt.Errorf(&quot;after 10 tries error: %v&quot;, err)
}

答案2

得分: 3

让我们看看http.NewRequest是做什么的。

rc, ok := body.(io.ReadCloser)
if !ok && body != nil {
    rc = ioutil.NopCloser(body)
}
// 主机的冒号:端口应该被规范化。参见问题14836。
u.Host = removeEmptyPort(u.Host)
req := &Request{
    Method:     method,
    URL:        u,
    Proto:      "HTTP/1.1",
    ProtoMajor: 1,
    ProtoMinor: 1,
    Header:     make(Header),
    Body:       rc,
    Host:       u.Host,
}

body的类型是io.Reader,通过ioutil.NopCloser转换为io.ReadCloser。正如@Cerise Limón所说,Request.Body已经被读取并关闭了,所以当你再次调用Do()时,Body Length为0。

因此,在调用Do之前,我们可以重置Request.Body。

func Post(URL string, form url.Values, cl *http.Client) ([]byte, error) {
    requestBodyString := form.Encode()
    req, err := http.NewRequest("POST", URL, strings.NewReader(requestBodyString))
    // ...

    rsp, err := do(cl, req, requestBodyString)

    //...
    return b, nil
}

func do(cl *http.Client, req *http.Request, requestBodyString string)(*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)
        // 重置Request.Body
        req.Body = ioutil.NopCloser(strings.NewReader(requestBodyString))
        rsp, err = cl.Do(&req)
        if err == nil{
            return rsp, nil
        }
        if i > 10 {

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

以上是对代码的翻译。

英文:

Let's see what does http.NewRequest do

rc, ok := body.(io.ReadCloser)
if !ok &amp;&amp; body != nil {
	rc = ioutil.NopCloser(body)
}
// The host&#39;s colon:port should be normalized. See Issue 14836.
u.Host = removeEmptyPort(u.Host)
req := &amp;Request{
	Method:     method,
	URL:        u,
	Proto:      &quot;HTTP/1.1&quot;,
	ProtoMajor: 1,
	ProtoMinor: 1,
	Header:     make(Header),
	Body:       rc,
	Host:       u.Host,
}

body is type of io.Reader, and convert to io.ReaderCloser by ioutil.NopCloser. As @Cerise Limón said, the Request.Body had been read and the stream is closer, so when you Do() again, the Body Length is 0.

So, we could reset the Request.Body before invoke Do.

func Post(URL string, form url.Values, cl *http.Client) ([]byte, error) {
    requestBodyString := form.Encode()
    req, err := http.NewRequest(&quot;POST&quot;, URL, strings.NewReader(requestBodyString))
    // ...

    rsp, err := do(cl, req, requestBodyString)

    //...
    return b, nil
}

func do(cl *http.Client, req *http.Request, requestBodyString string)(*http.Response, error){
    rsp, err := cl.Do(req)
    for i := 0; IsErrProxy(err); i++ {
        log.Errorf(&quot;Proxy is slow or down &quot;)
        time.Sleep(6 * time.Second)
        // reset Request.Body
        req.Body = ioutil.NopCloser(strings.NewReader(requestBodyString))
        rsp, err = cl.Do(&amp;req)
        if err == nil{
            return rsp, nil
        }
        if i &gt; 10 {

            return nil, fmt.Errorf(&quot;after %v tries error: %v&quot;, i, err)
        }
    }
    return rsp, err
}

答案3

得分: 0

我找到了一种方法,可以在不每次重新创建请求的情况下完成这个操作。以下是一个示例代码,它是对 @Cerise Limón 的代码进行了非常轻微的修改,并且与 @Rambo 的代码类似,只创建了一次请求:

func Post(URL string, data *bytes.Buffer, cl *http.Client) ([]byte, error) {
	var err error

	req, err := http.NewRequest("POST", URL, ioutil.NopCloser(data))
	if err != nil {
		log.Errorf("无法创建请求:%v", err)
		return nil, err
	}
	req.Header.Set("User-Agent", ua)

	for i := 0; i < 10; i++ {
		rsp, err := cl.Do(req)
		if err == nil && rsp.StatusCode == 200 {
			defer rsp.Body.Close()
			b, err := ioutil.ReadAll(rsp.Body)
			if err != nil {
				fmt.Printf("错误:%v", err)
				return nil, err
			}
			return b, nil
		}

		log.Errorf("代理速度慢或不可用:%v", err)
		time.Sleep(1 * time.Second)
	}
	return nil, fmt.Errorf("尝试10次后出错:%v", err)
}

func main() {
	client := http.Client{}
	data := []byte{0, 1, 2, 3, 4}
	Post("http://server/my/api/resource/", bytes.NewBuffer(data), &client)
}
英文:

I found a way to do this without re-creating the request every time. here's a sample code, which is a very slight modification of @Cerise Limón and is similar to @Rambo's code in that it creates the request only once:

func Post(URL string, data *bytes.Buffer, cl *http.Client) ([]byte, error) {
	var err error

	req, err := http.NewRequest(&quot;POST&quot;, URL, ioutil.NopCloser(data))
	if err != nil {
		log.Errorf(&quot;Unable to create the request: %v&quot;, err)
		return nil, err
	}
	req.Header.Set(&quot;User-Agent&quot;, ua)

	for i := 0; i &lt; 10; i++ {
		rsp, err := cl.Do(req)
		if err == nil &amp;&amp; rsp.StatusCode == 200 {
			defer rsp.Body.Close()
			b, err := ioutil.ReadAll(rsp.Body)
			if err != nil {
				fmt.Printf(&quot;Error: %v&quot;, err)
				return nil, err
			}
			return b, nil
		}

		log.Errorf(&quot;Proxy is slow or down %v&quot;, err)
		time.Sleep(1 * time.Second)
	}
	return nil, fmt.Errorf(&quot;after 10 tries error: %v&quot;, err)
}

func main() {
	client := http.Client{}
	data := []byte{0, 1, 2, 3, 4}
	Post(&quot;http://server/my/api/resource/&quot;, bytes.NewBuffer(data), &amp;client)
}

huangapple
  • 本文由 发表于 2015年7月10日 17:57:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/31337891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定