Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go net/http传输接受编码:gzip不提示编码响应_Http_Go - Fatal编程技术网

Go net/http传输接受编码:gzip不提示编码响应

Go net/http传输接受编码:gzip不提示编码响应,http,go,Http,Go,可能与 我知道net/http传输将向请求添加Accept Encoding:gzip,除非DisableCompression设置为True,如果我希望它自动解压缩gzip响应,这是必需的。在这种情况下,以下代码不接收内容编码:gzip头: (注意:由于网络限制,不会在操场上跑步) 如果我运行一个本地服务器并运行上面的代码,我可以看到发送的预期标头: GET / HTTP/1.1 Host: localhost:5555 User-Agent: Go-http-client/1.1 Accep

可能与

我知道net/http传输将向请求添加
Accept Encoding:gzip
,除非
DisableCompression
设置为True,如果我希望它自动解压缩gzip响应,这是必需的。在这种情况下,以下代码不接收
内容编码:gzip
头:
(注意:由于网络限制,不会在操场上跑步)

如果我运行一个本地服务器并运行上面的代码,我可以看到发送的预期标头:

GET / HTTP/1.1
Host: localhost:5555
User-Agent: Go-http-client/1.1
Accept-Encoding: gzip

go创建的连接是否还有其他原因导致服务器不返回gzip响应?

接受编码:gzip
仅表示客户端能够处理gzip压缩内容这并不意味着服务器必须实际压缩内容。事实上,例如,对于图像,使用gzip压缩内容没有任何意义,因为这些内容已经是压缩数据(但不是使用gzip),在上面添加gzip实际上可能会增加有效负载的大小

如果希望服务器返回压缩内容,实际上必须配置服务器来执行此操作。请参见如何使用nginx执行此操作的示例

请注意,
http.Response
透明地解压缩响应,并相应地更新标题,即删除
内容编码
。这意味着在使用
resp.header.get(“内容编码”)
进行检查时,将无法获得原始响应标题。如果响应是自动解压缩的,可以在
未压缩
字段中看到:

fmt.Println("was compressed ", resp.Uncompressed)
有关更多信息,请参见《go doc http.Response》:

// Uncompressed reports whether the response was sent compressed but
// was decompressed by the http package. When true, reading from
// Body yields the uncompressed content instead of the compressed
// content actually set from the server, ContentLength is set to -1,
// and the "Content-Length" and "Content-Encoding" fields are deleted
// from the responseHeader. To get the original response from
// the server, set Transport.DisableCompression to true.
Uncompressed bool

Accept Encoding:gzip
仅表示客户端能够处理压缩的gzip内容这并不意味着服务器必须实际压缩内容。事实上,例如,对于图像,使用gzip压缩内容没有任何意义,因为这些内容已经是压缩数据(但不是使用gzip),在上面添加gzip实际上可能会增加有效负载的大小

如果希望服务器返回压缩内容,实际上必须配置服务器来执行此操作。请参见如何使用nginx执行此操作的示例

请注意,
http.Response
透明地解压缩响应,并相应地更新标题,即删除
内容编码
。这意味着在使用
resp.header.get(“内容编码”)
进行检查时,将无法获得原始响应标题。如果响应是自动解压缩的,可以在
未压缩
字段中看到:

fmt.Println("was compressed ", resp.Uncompressed)
有关更多信息,请参见《go doc http.Response》:

// Uncompressed reports whether the response was sent compressed but
// was decompressed by the http package. When true, reading from
// Body yields the uncompressed content instead of the compressed
// content actually set from the server, ContentLength is set to -1,
// and the "Content-Length" and "Content-Encoding" fields are deleted
// from the responseHeader. To get the original response from
// the server, set Transport.DisableCompression to true.
Uncompressed bool

我理解并同意您所说的,但我的代码示例包括一个请求,如果客户端在请求中提供
Accept Encoding:gzip
,则该请求是一个返回gzip响应的平台。我的问题是指go似乎发送了正确的标题,但我没有得到Gzip响应和标题。@jonhadfield:that
https://golang.org
通常会返回压缩内容,这是我在您的问题中没有看到的细节。我已经更新了答案以添加其他信息,特别是go将自动解压缩响应并更新响应标题,这样您就不会获得服务器发送的原始
内容编码。谢谢Steffen。由于缺少内容编码头,我错误地认为响应是未压缩返回的。我理解并同意您的说法,但我的代码示例包括一个请求,如果客户端在请求中提供
Accept Encoding:gzip
,该平台将返回gzip响应。我的问题是指go似乎发送了正确的标题,但我没有得到Gzip响应和标题。@jonhadfield:that
https://golang.org
通常会返回压缩内容,这是我在您的问题中没有看到的细节。我已经更新了答案以添加其他信息,特别是go将自动解压缩响应并更新响应标题,这样您就不会获得服务器发送的原始
内容编码。谢谢Steffen。由于缺少内容编码头,我错误地认为响应是未压缩返回的。