Apache HTTP中的代理链接

时间:2012-03-14 09:18:53

标签: java apache-httpcomponents

对这里实际可能的事情感到困惑。

Java Apache HTTP Client(4.x)链代理可以吗?关于如何提示?

我发现documentation suggesting it can但是来源有点复杂,我发现至少有一个类(DefaultRequestDirector)抛出异常;

    throw new HttpException("Proxy chains are not supported.")

使用

配置具有单个代理的客户端是直接的
    httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

但对我来说如何设置代理链并不明显。如果我按照上面文档中的提示操作,请执行以下操作。

    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);
        }
    });

但这会导致上述异常;

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

1 个答案:

答案 0 :(得分:-1)

在您链接到上面的文档中,它说:

QUOTE 2.7。 HttpClient代理配置 尽管HttpClient知道复杂的路由场景和代理链,但它只支持开箱即用的简单直接或一跳代理连接。 引文结束

所以答案是开箱即用的,它无法处理代理链。