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.

The simplest way to tell HttpClient to connect to the target host via a proxy is by setting the default proxy parameter:

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

One can also instruct HttpClient to use the standard JRE proxy selector to obtain proxy information:

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

Alternatively, one can provide a custom RoutePlanner implementation in order to have a complete control over the process of HTTP route computation:

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

如果想针对不同的请求设置不同的代理,可以通过 RequestConfig 设置代理,然后在执行请求时带上 RequestConfig 参数。

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpHost proxy = new HttpHost("someproxy", 8080);
RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); HttpGet httpGet = new HttpGet("http://example.com");
httpGet.setConfig(config);
CloseableHttpResponse response = httpClient.execute(httpGet);

HttpClient(4.3.5) - HttpClient Proxy Configuration的更多相关文章

  1. HttpClient(一)HttpClient抓取网页基本信息

    一.HttpClient简介 HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包, 并且它支 ...

  2. httpclient妙用一 httpclient作为客户端调用soap webservice(转)

    前面有一篇使用HttpClient调用带参数的post接口方法,这里找到一篇使用HttpClient调用Soap协议接口的方式. 原文地址:httpclient妙用一 httpclient作为客户端调 ...

  3. Jmeter-Maven-Plugin高级应用:Proxy Configuration

    Proxy Configuration Pages 12 Home Adding additional libraries to the classpath Advanced Configuratio ...

  4. HttpClient(二)HttpClient使用Ip代理与处理连接超时

    前言 其实前面写的那一点点东西都是轻轻点水,其实HttpClient还有很多强大的功能: (1)实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等) (2)支持自动转向 (3)支持 ...

  5. Jersey(1.19.1) - Client API, Proxy Configuration

    为 Jersey Client 设置代理,可以使用带有 ClientHandler 参数的构造方法创建 Client 实例. public static void main(String[] args ...

  6. Java:HttpClient篇,HttpClient4.2在Java中的几则应用:Get、Post参数、Session(会话)保持、Proxy(代理服务器)设置,多线程设置...

    新版HttpClient4.2与之前的3.x版本有了很大变化,建议从http://hc.apache.org/处以得到最新的信息. 关于HttpCore与HttpClient:HttpCore是位于H ...

  7. Table of Contents - HttpClient

    HttpClient 4.3.5 Getting Started HttpClient 简单示例 Fundamentals Request Execution HTTP Request & H ...

  8. HttpClient 专题

    HttpClient is a HTTP/1.1 compliant HTTP agent implementation based on HttpCore. It also provides reu ...

  9. 关于 C# HttpClient的 请求

    Efficiently Streaming Large HTTP Responses With HttpClient Downloading large files with HttpClient a ...

随机推荐

  1. 任务分发系统gearman

    1 Gearman是什么 Gearman Job Server@http://gearman.org/. Gearman 是一个任务分发系统,它提供了一个分发框架,能够分发某类任务到更适合处理这类任务 ...

  2. UVA 11134 - Fabled Rooks(贪心+优先队列)

    We would like to place  n  rooks, 1 ≤  n  ≤ 5000, on a  n×n  board subject to the following restrict ...

  3. 通过lldb远程调试iOS App

    苹果从Xcode5开始弃用了gcc及gdb, 只能使用llvm用lldb. 在越狱机上虽然仍然可以使用gdb进行调试,但lldb是趋势.下面就介绍一种通过Wifi或者USB,在Mac上使用lldb对i ...

  4. 怎么SDCard上的获取相册照片

    private String getRealPathFromURI(Uri contentUri) { Cursor cursor = null; String result = contentUri ...

  5. V​M​W​a​r​e​里​安​装​6​4​位​L​i​n​u​x​ ​的​方​法

    1.CPU AMD系列的CPU略过 Intel系列的CPU芯片需要支持EM64T和VT技术才行,并且BIOS也要支持才可以. 为了确定你的Intel CPU是否支持VT,请查看: http://com ...

  6. Codeforces Testing Round #12 A. Divisibility 水题

    A. Divisibility Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...

  7. [WebGL] Setting Up WebGL

    In this lesson we cover setting up WebGL for use, including creating a canvas, getting the WebGL ren ...

  8. sql操作事务SqlTransHelper类实现

    具体实现代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...

  9. WebBrowser 禁用脚本错误提示

    public partial class Text : UserControl {        public Text()        {                              ...

  10. Linux 引导过程内幕

    转载:http://www.ibm.com/developerworks/cn/linux/l-linuxboot/index.html   从主引导记录到第一个用户空间应用程序的指导 引导 Linu ...