1. If single proxy for all targets is enough for you:

    HttpComponentsClientHttpRequestFactory clientHttpRequestFactory
    = new HttpComponentsClientHttpRequestFactory(
    HttpClientBuilder.create()
    .setProxy(new HttpHost("myproxy.com", 80, "http"))
    .build());
    restTemplate = new RestTemplate(clientHttpRequestFactory);
  2. Or if you want to use different proxies for different target URIs, schemas, etc. you can useHttpRoutePlanner with custom ProxySelector:

    HttpRoutePlanner routePlanner = new SystemDefaultRoutePlanner(new MyProxySelector());
    
    HttpComponentsClientHttpRequestFactory clientHttpRequestFactory
    = new HttpComponentsClientHttpRequestFactory(
    HttpClientBuilder.create()
    .setRoutePlanner(routePlanner)
    .build());
    restTemplate = new RestTemplate(clientHttpRequestFactory);
  3. Example proxy selector: MyProxySelector.java:
  4. package hello;
    
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.net.Proxy;
    import java.net.Proxy.Type;
    import java.net.ProxySelector;
    import java.net.SocketAddress;
    import java.net.URI;
    import java.util.ArrayList;
    import java.util.List; public class MyProxySelector extends ProxySelector { ProxySelector defaultproxySelector = ProxySelector.getDefault(); ArrayList<Proxy> noProxy = new ArrayList<Proxy>();
    ArrayList<Proxy> secureProxy = new ArrayList<Proxy>();
    ArrayList<Proxy> sociaMediaProxy = new ArrayList<Proxy>(); public MyProxySelector(){ noProxy.add(Proxy.NO_PROXY); secureProxy.add(new Proxy(Type.HTTP, new InetSocketAddress(
    "secure.proxy.mycompany.com", 8080))); sociaMediaProxy.add(new Proxy(Type.HTTP, new InetSocketAddress(
    "social-media.proxy.mycompany.com", 8080)));
    } @Override
    public List<Proxy> select(URI uri) { // No proxy for local company addresses.
    if ( uri.getHost().toLowerCase().endsWith("mycompany.com") ) {
    return noProxy ;
    } // Special proxy for social networks.
    String host = uri.getHost().toLowerCase();
    if ( host.endsWith("facebook.com") ||
    host.endsWith("twitter.com") ||
    host.endsWith("cfapps.io") ||
    host.endsWith("flickr.com") )
    {
    return sociaMediaProxy ;
    } // for https URIs use secureProxy
    if ( uri.getScheme().toLowerCase().equals("https") ){
    return secureProxy ;
    } if (defaultproxySelector != null) {
    return defaultproxySelector.select(uri);
    } return noProxy;
    } @Override
    public void connectFailed(URI arg0, SocketAddress arg1, IOException arg2) {
    // TODO Auto-generated method stub
    }
    }

httpclient设置proxy与proxyselector的更多相关文章

  1. 通过httpClient设置代理Ip

    背景: 我们有个车管系统,需要定期的去查询车辆的违章,之前一直是调第三方接口去查,后面发现数据不准确(和深圳交警查的对不上),问题比较多.于是想干脆直接从深圳交警上查,那不就不会出问题了吗,但是问题又 ...

  2. HttpClient 设置代理方式

    HttpClient httpClient = new HttpClient(); //设置代理服务器的ip地址和端口 httpClient.getHostConfiguration().setPro ...

  3. httpclient: 设置请求的超时时间,连接超时时间等

    httpclient: 设置请求的超时时间,连接超时时间等 public static void main(String[] args) throws Exception{ //创建httpclien ...

  4. HttpClient设置代理,超时,以及得到cookies

    import java.net.URI; import java.util.List; import org.apache.http.HttpEntity; import org.apache.htt ...

  5. 实现代理设置proxy

    用户在哪些情况下是需要设置网络代理呢? 1. 内网上不了外网,需要连接能上外网的内网电脑做代理,就能上外网:多个电脑共享上外网,就要用代理: 2.有些网页被封,通过国外的代理就能看到这被封的网站:3. ...

  6. Django如何设置proxy

    设置porxy的原因 一般情况下我们代理设置是针对与浏览器而言,通常只需在浏览器设置中进行配置,但它只针对浏览器有效,对我们自己编写的程序并任何效果,这时就需要我们在软件编码中加入代理设置. --- ...

  7. 设置Proxy Server和SQL Server实现互联网上的数据库安全

    ◆首先,我们需要了解一下SQL Server在WinSock上定义协议的步骤: 1. 在”启动”菜单上,指向”程序/Microsoft Proxy Server”,然后点击”Microsoft Man ...

  8. C# HttpClient设置cookies的两种办法 (转发)

    一般有两种办法 第一种handler.UseCookies=true(默认为true),默认的会自己带上cookies,例如 var handler = new HttpClientHandler() ...

  9. selenium设置proxy、headers(phantomjs、Chrome、Firefox)

    phantomjs 设置ip 方法1: service_args = [ '--proxy=%s' % ip_html, # 代理 IP:prot (eg:192.168.0.28:808) '--p ...

随机推荐

  1. Linux——配置使用github

    前一段时间在windows下配置了github的环境,参考“TortoiseGit连接github.com”一文,现在学习在linux下编程,在网上找了点资料,配置在linux下使用github,将过 ...

  2. Odoo8查询产品时提示"maximum recursion depth exceeded while calling a Python object"

    今天在生产系统中查询产品时,莫名提示错误:maximum recursion depth exceeded while calling a Python object,根据错误日志提示,发现在查询产品 ...

  3. 【Python3 爬虫】11_报错No module named 'requests'

    从网上下载了一段源码,执行过程中报错: No module named 'requests' 一看英文就明白是咋回事了~ 是由于:没有模块requests 解决方案 打开cmd,在窗口运行命令:pip ...

  4. XAOP的使用示范例子

    代码地址如下:http://www.demodashi.com/demo/12976.html XAOP 一个简易的AOP(Android)应用框架.囊括了最实用的AOP应用. 特点 支持快速点击切片 ...

  5. 2014哈商大ICPC/ACM校赛解题报告

    被debug邀请去參加校赛,哎,被虐..我对不起工大.. 由于本人不搞ACM,算法处于HelloWorld水准.. 虽然题目除了鸟不拉屎星人之外都非常水,但我能做到这个程度,全然是超水平发挥了.. 数 ...

  6. 由于删除DBF文件报错 —— ORA-01033: ORACLE initialization or shutdown in progress

    由于移动或删除DBF文件报错:ORA-01033: ORACLE initialization or shutdown in progress   原因:一般该类故障通常是由于移动文件而影响了数据库日 ...

  7. mysql添加修改字段

    ALTER TABLE `uc_organization` ADD COLUMN `agent_id` VARCHAR(50) NOT NULL DEFAULT 0 COMMENT 'sqlserve ...

  8. WPF入门教程系列一

    WPF入门教程 一.  前言  公司项目基于WPF开发,最近项目上线有点空闲时间写一篇基于wpf的基础教材,WPF也是近期才接触,学习WPF也是在网上查资料与微软的MSDN进行学习,写本博客的目为了温 ...

  9. sql中的笛卡尔积

    sql中的笛卡尔积 我们对数据库表进行操作时,经常会对多张表进行关联,多表连接查询大家肯定不会陌生,但是一不小心很容易出来庞大冗余的数据. 笛卡尔积数学概念 笛卡尔积是指在数学中,两个集合X和Y的笛卡 ...

  10. 关于http和rpc的区别(segmentfault上的回答)

    问题最近用了谷歌的grpc,所以对rpc和http有一点疑惑,感觉这两个东西功能上是一样的,rpc某个服务监听某一个方法,客户端调用这个方法,返回相应的数据,和http监听某个方法的路由 返回相应的数 ...