Go:http包的一个坑

news/2024/5/18 15:33:50 标签: web, http, python, html, bbs
htmledit_views">

问题描述:

使用client.do(....)循环发送html" title=http>http 消息的时候,会报html" title=http>http: ContentLength=27 with Body length 0 错误。

原因是:

client.do(...)函数中发送完一次req之后,会将req中的body清空导致的。

func (c *Client) do(req *Request) (retres *Response, reterr error) {

    ....

    req.closeBody()

   ....

}

问题复现例子:

package main


import (
        "fmt"
        "net/html" title=http>http"
        "time"
        "bytes"
)


func main() {
        tr := &html" title=http>http.Transport{
            MaxIdleConns:       10,
            IdleConnTimeout:    30 * time.Second,
            DisableCompression: true,
        }
        client := &html" title=http>http.Client{Transport: tr}
        str := "hello world, I am html" title=http>http body"
        // 这里的req是全局的,所有的发送消息都会使用这个req
        req, err := html" title=http>http.NewRequest("GET", "html" title=http>http://127.0.0.1/", bytes.NewReader([]byte(str)))
        if err != nil {
            fmt.Println(err)
            return
        }
        req.Header.Add("If-None-Match", `W/"wyzzy"`)
        for i:=0;i<5;i++ {
            resp, err := client.Do(req)
            if err != nil {
                fmt.Println(err)
            }
            fmt.Printf("%s \n", resp)
        }
}

输出结果:

$ ./client //第一次发送成功,后面发送都会失败。
&{200 OK %!s(int=200) HTTP/1.1 %!s(int=1) %!s(int=1) map[Content-Length:[23] Content-Type:[text/plain; charset=utf-8] Date:[Tue, 22 Dec 2020 09:19:43 GMT]] %!s(*html" title=http>http.bodyEOFSignal=&{0xc000090040 {0 0} false <nil> 0x12348c0 0x1234850}) %!s(int64=23) [] %!s(bool=false) %!s(bool=false) map[] %!s(*html" title=http>http.Request=&{GET 0xc000132000 HTTP/1.1 1 1 map[If-None-Match:[W/"wyzzy"]] {0xc000066c30} 0x1233870 27 [] false 127.0.0.1 map[] map[] <nil> map[]   <nil> <nil> <nil> 0xc000016088}) %!s(*tls.ConnectionState=<nil>)}
Get "html" title=http>http://127.0.0.1/": html" title=http>http: ContentLength=27 with Body length 0
%!s(*html" title=http>http.Response=<nil>)
Get "html" title=http>http://127.0.0.1/": html" title=http>http: ContentLength=27 with Body length 0
%!s(*html" title=http>http.Response=<nil>)
Get "html" title=http>http://127.0.0.1/": html" title=http>http: ContentLength=27 with Body length 0
%!s(*html" title=http>http.Response=<nil>)
Get "html" title=http>http://127.0.0.1/": html" title=http>http: ContentLength=27 with Body length 0
%!s(*html" title=http>http.Response=<nil>)

代码修复例子:

package main


import (
        "fmt"
        "net/html" title=http>http"
        "time"
        "bytes"
)


func main() {
        tr := &html" title=http>http.Transport{
            MaxIdleConns:       10,
            IdleConnTimeout:    30 * time.Second,
            DisableCompression: true,
        }
        client := &html" title=http>http.Client{Transport: tr}
        str := "hello world, I am html" title=http>http body"


        for i:=0;i<5;i++ {
            req, err := html" title=http>http.NewRequest("GET", "html" title=http>http://127.0.0.1/", bytes.NewReader([]byte(str)))
            if err != nil {
                fmt.Println(err)
                return
            }
            req.Header.Add("If-None-Match", `W/"wyzzy"`)
            resp, err := client.Do(req)
            if err != nil {
                fmt.Println(err)
            }
            fmt.Printf("%s \n", resp)
        }
}

修复后的执行结果:

$ ./client
&{200 OK %!s(int=200) HTTP/1.1 %!s(int=1) %!s(int=1) map[Content-Length:[23] Content-Type:[text/plain; charset=utf-8] Date:[Tue, 22 Dec 2020 09:23:42 GMT]] %!s(*html" title=http>http.bodyEOFSignal=&{0xc0001462c0 {0 0} false <nil> 0x12348c0 0x1234850}) %!s(int64=23) [] %!s(bool=false) %!s(bool=false) map[] %!s(*html" title=http>http.Request=&{GET 0xc00016a000 HTTP/1.1 1 1 map[If-None-Match:[W/"wyzzy"]] {0xc000114c30} 0x1233870 27 [] false 127.0.0.1 map[] map[] <nil> map[]   <nil> <nil> <nil> 0xc00012e008}) %!s(*tls.ConnectionState=<nil>)}
&{200 OK %!s(int=200) HTTP/1.1 %!s(int=1) %!s(int=1) map[Content-Length:[23] Content-Type:[text/plain; charset=utf-8] Date:[Tue, 22 Dec 2020 09:23:42 GMT]] %!s(*html" title=http>http.bodyEOFSignal=&{0xc00009c000 {0 0} false <nil> 0x12348c0 0x1234850}) %!s(int64=23) [] %!s(bool=false) %!s(bool=false) map[] %!s(*html" title=http>http.Request=&{GET 0xc00016a200 HTTP/1.1 1 1 map[If-None-Match:[W/"wyzzy"]] {0xc0001150e0} 0x1233870 27 [] false 127.0.0.1 map[] map[] <nil> map[]   <nil> <nil> <nil> 0xc00012e008}) %!s(*tls.ConnectionState=<nil>)}
&{200 OK %!s(int=200) HTTP/1.1 %!s(int=1) %!s(int=1) map[Content-Length:[23] Content-Type:[text/plain; charset=utf-8] Date:[Tue, 22 Dec 2020 09:23:42 GMT]] %!s(*html" title=http>http.bodyEOFSignal=&{0xc0001a4000 {0 0} false <nil> 0x12348c0 0x1234850}) %!s(int64=23) [] %!s(bool=false) %!s(bool=false) map[] %!s(*html" title=http>http.Request=&{GET 0xc0000ae000 HTTP/1.1 1 1 map[If-None-Match:[W/"wyzzy"]] {0xc000096180} 0x1233870 27 [] false 127.0.0.1 map[] map[] <nil> map[]   <nil> <nil> <nil> 0xc00012e008}) %!s(*tls.ConnectionState=<nil>)}
&{200 OK %!s(int=200) HTTP/1.1 %!s(int=1) %!s(int=1) map[Content-Length:[23] Content-Type:[text/plain; charset=utf-8] Date:[Tue, 22 Dec 2020 09:23:42 GMT]] %!s(*html" title=http>http.bodyEOFSignal=&{0xc00009c100 {0 0} false <nil> 0x12348c0 0x1234850}) %!s(int64=23) [] %!s(bool=false) %!s(bool=false) map[] %!s(*html" title=http>http.Request=&{GET 0xc00016a380 HTTP/1.1 1 1 map[If-None-Match:[W/"wyzzy"]] {0xc000115440} 0x1233870 27 [] false 127.0.0.1 map[] map[] <nil> map[]   <nil> <nil> <nil> 0xc00012e008}) %!s(*tls.ConnectionState=<nil>)}
&{200 OK %!s(int=200) HTTP/1.1 %!s(int=1) %!s(int=1) map[Content-Length:[23] Content-Type:[text/plain; charset=utf-8] Date:[Tue, 22 Dec 2020 09:23:42 GMT]] %!s(*html" title=http>http.bodyEOFSignal=&{0xc0001a4080 {0 0} false <nil> 0x12348c0 0x1234850}) %!s(int64=23) [] %!s(bool=false) %!s(bool=false) map[] %!s(*html" title=http>http.Request=&{GET 0xc0000ae180 HTTP/1.1 1 1 map[If-None-Match:[W/"wyzzy"]] {0xc000096510} 0x1233870 27 [] false 127.0.0.1 map[] map[] <nil> map[]   <nil> <nil> <nil> 0xc00012e008}) %!s(*tls.ConnectionState=<nil>)}

http://www.niftyadmin.cn/n/873010.html

相关文章

php复制独立数组,是否有一个函数将PHP数组的副本复制到另一个?

是否有一个函数将PHP数组的副本复制到另一个&#xff1f;我已经烧了几次试图复制PHP数组。 我想将对象内定义的数组复制到其外部的全局。#1楼array_merge()是一个可以在PHP中将一个数组复制到另一个数组的函数。#2楼当你这样做$array_x $array_y;PHP复制数组&#xff0c;所以我…

TCP的保活定时器

在开始这边文章之前&#xff0c;我问了自己几个问题&#xff0c;如下所示&#xff1a;为什么我们需要保活消息&#xff1f;保活消息有什么优缺点&#xff1f;保活消息是怎么使用的&#xff1f;下面我们来一个个问题看下去。1. 为什么我们需要保活消息&#xff1f;对于TCP链接来…

redis实际操作部署篇(二)

写在前面的话&#xff1a;对于redis来说&#xff0c;它有四种部署模式&#xff0c;分别是单机模式、主从模式、哨兵模式和集群模式&#xff0c;他们的使用场景有些区别&#xff0c;当然也是越来越复杂&#xff0c;可靠性越来越高。本文从实际操作的角度&#xff0c;来介绍和讲解…

javascript uri 编码

对比 javascript url编码 javascript 常用的编码格式有&#xff1a;escape(), encodeURL(), encodeURIComponent() 区别如下&#xff1a; escape() 方法&#xff1a; 采用ISO Latin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转…

oracle动态修改表结构,ORACLE修改表结构

1&#xff0c;增加新字段&#xff1a;alter table table_nameadd (name varchar(20) default unkown);如&#xff1a;SQL> run;1 alter table test32* add(address varchar(100))表已更改。2&#xff0c;修改表字段&#xff1a;alter table table_namemodify (name varchar2…

php 循环输出数组下表,关于js数组循环输出的几个方法以及关于下标不固定循环输出控制的方法...

今天上班遇到个下标不固定的jsjson对象,如图本人想实现的是忽略第一个元素,循环后面的数据,一开始使用的是for循环var i;for(i1;i}没写完就发现这样不行,因为本身的下标是2,data[i],当i1时是没有值的后来试了下$.each$.each(data,function(i,n){if(i>1) {console.log(i , …

Raft中的领导选取

Raft 是一种用来管理日志复制的一致性算法。为了提高理解性&#xff0c;Raft 将一致性算法分为了几个部分&#xff0c;例如领导选取&#xff08;leader selection&#xff09;&#xff0c;日志复制&#xff08;log replication&#xff09;和安全性&#xff08;safety&#xff…

session、cookie、隐藏域、url参数传递四种会话及跟踪方式

1.Session HttpSession session request.getSession(); session.setAttribute("name", "zhangsan"); session.setAttribute("pwd", "aaa"); String name (String) session.getAttribute("name"); 2.cookie: ? 设置 Co…