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. android EditText inputType 及 android:imeOptions=”actionDone”

    一.android 软件盘事件响应 在android中,有时需要对EditText实现软件盘监听的场景.当android按下软键盘的时候,响应完成.发送.搜索或者其他事件. Google 提供了 Ed ...

  2. POJ3525-Most Distant Point from the Sea(二分+半平面交)

    Most Distant Point from the Sea Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3955   ...

  3. RAID详解[RAID0/RAID1/RAID10/RAID5] (转)

    一.RAID定义RAID(Redundant Array of Independent Disk 独立冗余磁盘阵列)技术是加州大学伯克利分校1987年提出,最初是为了组合小的廉价磁盘来代替大的昂贵磁盘 ...

  4. LoadRunner+Java接口性能测试

    想必各位小伙伴们会对LR还可以调用java感到好奇,之前我也这么一直认为LR只支持C语言.其实LR脚本支持的语言有:C.Java.Visual Basic.VbScript.JavaScript,只不 ...

  5. WPF入门教程系列一

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

  6. Ioc(控制反转)、DI(依赖注入)

    一篇非常好的有关控制反转和依赖注入非常不错的文章,简单易通,与大家共同学习,这里只引用了一篇文章,还有很多相关的文章可以通过文章引用地址来看,相信大家看完理解的就比较深刻了 文章摘自:http://j ...

  7. 本文演示如何配置ASP.NET Core项目以在Visual Studio(VS)2017中使用Telerik UI for ASP.NET Core。

    学习时使用的是VS2017+Core2.1了,不再讨论VS2015和core1.1的东西. 配置ASP.NET Core Web应用程序以使用Telerik UI for ASP.NET Core: ...

  8. Centos使用光盘作为本地yum源

    [root@localhost CentOS]# mkdir /media/CentOS把光盘加载到本地[root@localhost CentOS]# mount /dev/cdrom /media ...

  9. 使用CSS3实现响应式标题全屏居中和站点前端性能

    要实现标题全屏居中(同一时候在垂直和水平方向居中).有若干种方法,包含使用弹性布局.表格单元.绝对定位和自己主动外边距等. 全屏居中 当中眼下比較流行也比較easy理解的方法是使用绝对定位+偏移实现. ...

  10. 【SSH三大框架】Hibernate基础第九篇:cascade关联关系的级联操作

    这里要说的是Hibernate的关联关系的级联操作,使用cascade属性控制. 依旧用部门和员工举例.多个员工相应一个部门(多对一关联关系) 员工类:Employee.java package cn ...