Created
November 22, 2012 03:49
-
-
Save Qw4z1/4129361 to your computer and use it in GitHub Desktop.
Enable gzip compression with HttpClient. All code from the goole IOSched app.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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