使用httpclient 4.5.3的GZIP

问题描述 投票:0回答:1

我正在使用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

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

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

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

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

java gzip apache-httpclient-4.x apache-fluent-api
1个回答
3
投票

ResponseContentEncoding删除内容元数据标题,例如Content-LengthContent-EncodingContent-MD5,以确保客户端透明地解压缩的内容流匹配响应消息头中嵌入的元数据。这是故意的。如果要使用流畅的外观手动处理内容解压缩,则需要创建自定义请求执行程序。

Executor executor = Executor.newInstance(HttpClients.custom().disableContentCompression().build());
© www.soinside.com 2019 - 2024. All rights reserved.