Does Apache Commons HttpClient support GZIP?

2019-01-11 13:01发布

问题:

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).

回答1:

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



回答2:

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

org.apache.http.impl.client.ContentEncodingHttpClient

which is a subclass of DefaultHttpClient.

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



回答3:

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.

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

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



回答4:

Since 4.1, Apache HttpClients handles request and response compression.

  • 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 ResponseContentEncoding api doc here.

Just use:

HttpClients.custom()

which uses:

HttpClientBuilder.create()

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

You can disable it through "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();


回答5:

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

 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()
  }


回答6:

Custom Protocol Interceptors may help as well.

Disclaimer: I haven't tried this yet.



回答7:

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

val entity = new GzipDecompressingEntity(response.getEntity)

then proceed with entity.getContent as always.