Does Apache Commons HttpClient support GZIP?
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();
Solution 4
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.
Solution 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()
}
benstpierre
My entire career has been building commercial software mostly in Java and C#. I have working with a dozen other languages but I really love working in these two the most. Recently I have been writing application in Vaadin and think it's the best thing since sliced bread.
Updated on February 08, 2022Comments
-
benstpierre about 2 years
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).
-
skaffman about 14 years@karim79: I've abandoned any hope that HttpClient is being maintained by anyone with a grip on reality (as if the HttpClient 4 API wasn't evidence enough).
-
Hbf almost 12 yearsIn HttpClient 4.2.1,
ContentEncodingHttpClient
is deprecated; users are encouraged to use the DecompressingHttpClient, see hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/… -
djechlin over 10 yearshow do I take it out of the box?
-
djechlin over 10 years@Hbf also deprecated.
-
Jonik about 10 yearsStarting from 4.3,
HttpClientBuilder
should be used (instead ofContentEncodingHttpClient
orDecompressingHttpClient
). -
benkc over 7 yearsWith the
HttpClientBuilder
, do you have to call any particular methods on the builder to enable gzip? Or do you simply have to not calldisableContentCompression()
? -
Clement.Xu over 6 yearsNo, compression is automatically enabled. I tried it with 4.5.3 and used wireshark to capture the request the response: the "Accept-Encoding: gzip,deflate" header was added in the request automatically, and the response content can be automatically decompressed.
-
Clement.Xu over 6 yearsIn 4.5.3, just use a client by HttpClientBuilder.create().build(), and it will handle all the gzip request and response decompression for you.