《HttpClient官方文档》2.8 HttpClient代理配置

简介:

2.8. HttpClient代理配置

即使HttpClient意识到路由方案和代理连接的复杂性,它也只支持简单直连或单跳代理连接的开箱即用。

通知HttpClient连接到目标主机,最简单的方法是通过设置默认参数的代理:

HttpHost proxy = new HttpHost("someproxy", 8080);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
CloseableHttpClient httpclient = HttpClients.custom()
        .setRoutePlanner(routePlanner)
        .build();

还可以指示HttpClient使用标准的JRE代理选择器来获取代理信息:

SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(
        ProxySelector.getDefault());
CloseableHttpClient httpclient = HttpClients.custom()
        .setRoutePlanner(routePlanner)
        .build();

或者,可以利用customRoutePlanner接口的实现类来完全控制HTTP路由计算的过程:

HttpRoutePlanner routePlanner = new HttpRoutePlanner() {

    public HttpRoute determineRoute(
            HttpHost target,
            HttpRequest request,
            HttpContext context) throws HttpException {
        return new HttpRoute(target, null,  new HttpHost("someproxy", 8080),
                "https".equalsIgnoreCase(target.getSchemeName()));
    }

};
CloseableHttpClient httpclient = HttpClients.custom()
        .setRoutePlanner(routePlanner)
        .build();
    }
}
相关文章
|
6月前
HttpClient库与代理IP的应用
HttpClient库与代理IP的应用
|
JSON 数据格式
HttpClient远程调用基本使用(详解)
HttpClient远程调用基本使用(详解)
304 0
|
数据安全/隐私保护 数据格式 XML
HttpClient4.X的代理添加实现
HttpClient4.X的代理添加实现http://www.bieryun.com/1630.html [java] 查看纯文本 package  org.yla.test; import  java.
1201 0
|
Web App开发 编解码 Java