Does Apache Commons HttpClient support GZIP?
-
03-10-2019 - |
Pergunta
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).
Solução
Apache HttpClient 4.1 supports content compression out of the box along with many other features that were previously considered out of scope.
Outras dicas
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.
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.
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();
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()
}
Custom Protocol Interceptors may help as well.
Disclaimer: I haven't tried this yet.
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.