Java Apache Commons HttpClient 是否支持 GZIP?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2777076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 12:44:53  来源:igfitidea点击:

Does Apache Commons HttpClient support GZIP?

javagzipapache-commonsapache-httpclient-4.xapache-commons-httpclient

提问by benstpierre

Does the library Apache Commons HttpClient support Gzip? We wanted to use enable gzip compression on our Apache server to speed up the client/server communications (we have a php page that allows our Android application to sync files with the Server).

库 Apache Commons HttpClient 是否支持 Gzip?我们想在我们的 Apache 服务器上使用启用 gzip 压缩来加速客户端/服务器通信(我们有一个 php 页面,允许我们的 Android 应用程序与服务器同步文件)。

采纳答案by ok2c

Apache HttpClient 4.1 supports content compression out of the box along with many other features that were previously considered out of scope.

Apache HttpClient 4.1 支持开箱即用的内容压缩以及许多以前被认为超出范围的其他功能。

回答by skaffman

It has no support for this out-of-the-box, and it seems unlikely to be added to HttpClient 3.x (see rather bitchy JIRA issue here). You can, however, do it by adding custom request readers and manual request/response stream handling, layered on top of the basic library, but it's fiddly.

它不支持这种开箱即用的功能,并且似乎不太可能将其添加到 HttpClient 3.x(请参阅此处的JIRA 问题)。但是,您可以通过添加自定义请求读取器和手动请求/响应流处理来实现,这些处理位于基本库的顶部,但这很繁琐。

It seems you can do it with HttpClient 4, but not without some effort.

似乎您可以使用 HttpClient 4 来做到这一点,但并非不费吹灰之力。

Pretty shoddy, if you ask me, this stuff really should be easier than it is.

相当粗制滥造,如果你问我,这些东西真的应该比现在更容易。

回答by Denys

If your server is able to provide GZIPped content, with Apache Http client 4.1 all you need is to use

如果您的服务器能够提供 GZIP 压缩的内容,那么您只需要使用 Apache Http 客户端 4.1

org.apache.http.impl.client.ContentEncodingHttpClient

which is a subclass of DefaultHttpClient.

它是 的子类DefaultHttpClient

This client will also add headers saying that it accepts GZIPped content.

此客户端还将添加标头,说明它接受 GZIP 压缩的内容。

回答by James Selvakumar

Custom Protocol Interceptorsmay help as well.

自定义协议拦截器也可能有所帮助。

Disclaimer: I haven't tried this yet.

免责声明:我还没有尝试过这个。

回答by expert

It doesn't support it out of the box but you can transform entity of returned HttpResponseinto uncompressed one by calling

它不支持开箱即用,但您可以HttpResponse通过调用将返回的实体转换为未压缩的实体

val entity = new GzipDecompressingEntity(response.getEntity)

then proceed with entity.getContentas always.

然后entity.getContent像往常一样继续。

回答by Knight71

Here is the sample scala code which uses java apache-http-client library

这是使用 java apache-http-client 库的示例 scala 代码

 def createCloseableHttpClient(): CloseableHttpClient = {
    val builder: HttpClientBuilder = HttpClientBuilder.create
    val closableClient = builder.build()
    closableClient
  }

  def postData(data: String): Unit = {
    val entity = EntityBuilder.create()
      .setText(data)
      .setContentType(ContentType.TEXT_PLAIN)
      .gzipCompress()
      .build()
    val post = new HttpPost(postURL + endPoint)
    post.setEntity(entity)
    post.setHeader("Content-Type", "application/gzip")
    val client = createCloseableHttpClient()
    client.execute(post)
    client.close()
  }

回答by Garry

Since 4.1, Apache HttpClients handles request and response compression.

从 4.1 开始,Apache HttpClients 处理请求和响应压缩。

  • You don't need to compress request, no need to set any "Accept-Encoding" in request headers.
  • It automatically handles response decompression as well, no need to handle Decompression of response.
  • Till 4.3 it handles gzip and deflate. You can check ResponseContentEncodingapi doc here.
  • 您不需要压缩请求,不需要在请求头中设置任何“Accept-Encoding”。
  • 它也自动处理响应解压,无需处理响应解压。
  • 直到 4.3 它处理 gzip 和 deflate。您可以在此处查看ResponseContentEncodingapi 文档。

Just use:

只需使用:

HttpClients.custom()

which uses:

它使用:

HttpClientBuilder.create()

If you want to check in library goto HttpClientBuilderit uses RequestAcceptEncoding& ResponseContentEncoding

如果你想签入库 gotoHttpClientBuilder它使用RequestAcceptEncoding&ResponseContentEncoding

You can disable it through "disableContentCompression()"

您可以通过“disableContentCompression()”禁用它

HttpClient httpClient = HttpClients.custom()
                .setConnectionManager(cm)
                .disableContentCompression() //this disables compression
                .build();

Please make sure if you add any interceptor it can override that, use it carefully.

请确保如果您添加任何拦截器,它可以覆盖它,请谨慎使用它。

HttpClient httpClient = HttpClients.custom()
                .setConnectionManager(cm)
                .setHttpProcessor(httpprocessor) //this interceptor can override your compression.
                .build();