Skip to content

Instantly share code, notes, and snippets.

@Qw4z1
Created November 22, 2012 03:49
Show Gist options
  • Save Qw4z1/4129361 to your computer and use it in GitHub Desktop.
Save Qw4z1/4129361 to your computer and use it in GitHub Desktop.
Enable gzip compression with HttpClient. All code from the goole IOSched app.
private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
private static final String ENCODING_GZIP = "gzip";
final DefaultHttpClient client = new DefaultHttpClient(manager, parameters);
client.addRequestInterceptor(new HttpRequestInterceptor() {
public void process(HttpRequest request, HttpContext context) {
// Add header to accept gzip content
if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
}
}
});
client.addResponseInterceptor(new HttpResponseInterceptor() {
public void process(HttpResponse response, HttpContext context) {
// Inflate any responses compressed with gzip
final HttpEntity entity = response.getEntity();
final Header encoding = entity.getContentEncoding();
if (encoding != null) {
for (HeaderElement element : encoding.getElements()) {
if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
response.setEntity(new InflatingEntity(response.getEntity()));
break;
}
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment