GZIP 与 httpclient 4.5.3

use*_*811 1 java gzip apache-httpclient-4.x apache-fluent-api

我正在使用 httpclient 4.5.3 和 Fluent API ( http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fluent.html )。从 4.2.x 升级到 httpclient 4.5.3 后,响应中的“Content-Encoding”标头似乎从响应标头中删除,我不知道如何支持 gzip 压缩。

我正在向https://www.yahoo.com发出 GET 请求,并发送标头“Accept-Encoding”:“gzip”

我的 4.5.3 响应标头现在显示以下内容,但没有 Content-Encoding 标头:

Date: Thu, 01 Jun 2017 21:21:55 GMT
Strict-Transport-Security: max-age=2592000
X-Frame-Options: DENY
Set-Cookie: autorf=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/; domain=www.yahoo.com
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8
Age: 0
Transfer-Encoding: chunked
Connection: keep-alive
Via: http/1.1 ir11.fp.ne1.yahoo.com (ApacheTrafficServer)
Server: ATS
Cache-Control: no-store, no-cache, private, max-age=0
Expires: -1
Run Code Online (Sandbox Code Playgroud)

我的响应处理程序有以下代码。但是,entity.getContentEncoding() 始终为 null。

 HttpEntity entity = response.getEntity();
            Header header = entity.getContentEncoding();
            if (header == null) {
                return EntityUtils.toString(entity);
            } else {
                return handleGzipStream(entity);
            }
Run Code Online (Sandbox Code Playgroud)

通过调试器运行,看起来wrappedEntity的标头中确实有Content-Encoding:gzip。我如何访问它?或者使用 httpclient 4.5.3 处理 gzip 压缩的正确方法是什么?

[1]:https://i.stack.imgur.com/tyJ

ok2*_*k2c 5

ResponseContentEncoding会删除 、 等内容元数据标头Content-LengthContent-EncodingContent-MD5确保客户端透明解压的内容流与响应消息标头中嵌入的元数据相匹配。这是故意的。如果您想使用流畅的外观手动处理内容解压缩,您需要创建一个自定义请求执行器。

Executor executor = Executor.newInstance(HttpClients.custom().disableContentCompression().build());
Run Code Online (Sandbox Code Playgroud)