简体   繁体   中英

Proxy chaining in Apache HTTP

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM