6

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
skaffman
  • 398,947
  • 96
  • 818
  • 769
Toby
  • 9,523
  • 8
  • 36
  • 59

1 Answers1

-1

In the documentation you link to above it says:

QUOTE 2.7. HttpClient proxy configuration Even though HttpClient is aware of complex routing scemes and proxy chaining, it supports only simple direct or one hop proxy connections out of the box. UNQUOTE

So the answer is out of the box, it cannot handle proxy chains.

Simon G.
  • 6,587
  • 25
  • 30
  • 1
    If you look at the comments in the code though, a developer has outlined what could be done. So I don't think its as simple as 'it doesn't support it' and my question is how to coerce it. – Toby Mar 17 '12 at 12:08