Bit confused about what's actually possible here.
Can the Java Apache HTTP Client (4.x) chain proxies? Any tips as to how?
I've found documentation suggesting it can but the source is a little complicated and I've found at least one class (DefaultRequestDirector
) that throws an exception;
throw new HttpException("Proxy chains are not supported.")
It's straight forward to configure a client with a single proxy using
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
but its not obvious to me how to setup a chain of proxies. If I follow the hints on the documentation above I do the following.
httpClient.setRoutePlanner(new HttpRoutePlanner() {
@Override
public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
return new HttpRoute(target, null, new HttpHost[]{proxy, new HttpHost("localhost", 8081)}, "https".equalsIgnoreCase(target.getSchemeName()), TunnelType.TUNNELLED, LayerType.PLAIN);
}
});
but that causes the exception mentioned above;
org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:822)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
at Main.main(Main.java:70)
Caused by: org.apache.http.HttpException: Proxy chains are not supported.
at org.apache.http.impl.client.DefaultRequestDirector.createTunnelToProxy(DefaultRequestDirector.java:957)
at org.apache.http.impl.client.DefaultRequestDirector.establishRoute(DefaultRequestDirector.java:764)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:579)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:425)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
... 8 more