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


Solution 1:

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

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

Solution 3:

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