ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
DefaultHttpClient client = new DefaultHttpClient(cm, params);
// try resending the request once
client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(1, true));
// Add hooks for gzip/deflate
client.addRequestInterceptor(new HttpRequestInterceptor() {
public void process(
final org.apache.http.HttpRequest request,
final HttpContext context) throws HttpException, IOException {
if (!request.containsHeader("Accept-Encoding")) {
request.addHeader("Accept-Encoding", "gzip, deflate");
}
}
});
client.addResponseInterceptor(new HttpResponseInterceptor() {
public void process(
final org.apache.http.HttpResponse response,
final HttpContext context) throws HttpException, IOException {
HttpEntity entity = response.getEntity();
if (entity != null) {
Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
for (HeaderElement codec : ceheader.getElements()) {
String codecname = codec.getName();
if ("gzip".equalsIgnoreCase(codecname)) {
response.setEntity(
new GzipDecompressingEntity(response.getEntity()));
return;
} else if ("deflate".equals(codecname)) {
response.setEntity(new DeflateDecompressingEntity(response.getEntity()));
return;
}
}
}
}
}
});
client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler() );
// Use Java's built-in proxy logic
ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
client.getConnectionManager().getSchemeRegistry(),
ProxySelector.getDefault());