使用net/http包中的Get、Head、Post等函数,但不读body,也需要去关闭它吗?

GiXuan · · 2075 次点击
原来不管有没有读取Body部分都需要关闭呀,好,谢谢啦!
#4
更多评论
因该都不需要关闭手动关闭吧,下面是源码上面写的备注,说: The Server will close the request body. The ServeHTTP // Handler does not need to. Body io.ReadCloser // Body is the request's body. // // For client requests a nil body means the request has no // body, such as a GET request. The HTTP Client's Transport // is responsible for calling the Close method. // // For server requests the Request Body is always non-nil // but will return EOF immediately when no body is present. // The Server will close the request body. The ServeHTTP // Handler does not need to. Body io.ReadCloser 源码里面有如下: func (c *conn) serve() { .... serverHandler{c.server}.ServeHTTP(w, w.req) if c.hijacked() { return } w.finishRequest() if w.closeAfterReply { if w.requestBodyLimitHit { c.closeWriteAndWait() } break } .... } 处理完毕以后标准库会自动关闭链接的。
#1
你说的是服务端的关闭部分吧?如果我是作为客户端呢?
#2