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. 网络编程——C10K简述

    C10K问题   网络服务在处理数以万计的客户端连接时,往往出现效率底下甚至完全瘫痪,这被成为C10K问题. (C10K = connection 10 kilo 问题).k 表示 kilo,即 10 ...

  2. android Menory 小结

    不建议在Activity中使用static 变量,考虑使用Application.当然,static final例外 但Application也不要cache某个Activity使用的View,如果c ...

  3. Eclipse githup

    1:选择对应eclipse版本的githup组件版本,详见网址: http://wiki.eclipse.org/EGit/FAQ#Where_can_I_find_older_releases_of ...

  4. Zabbix触发器函数(取前后差值)

    获取最新值last zabbix触发器方法last用于获取item最新值或者第几个值以及某个时间的哪一个值. Last (most recent) T value is > N Last (mo ...

  5. 转:nolock的替代方案-提交读快照隔离[行版本控制]

    with(nolock)并意味着没有锁,实际上在查询一张表时,还是有锁,会对对象增加架构锁, 防止表会修改,会对数据库增加共享锁.若使用drop index,则要等到架构锁释放.   sql serv ...

  6. 将 ASP.NET Core 2.0 项目升级至 ASP.NET Core 2.1

    主要升级步骤如下: 将 .csproj 项目文件中的 target framework 改为 netcoreapp2.1 <TargetFramework>netcoreapp2.1< ...

  7. Java中super的几种使用方法并与this的差别

    1.     子类的构造函数假设要引用super的话,必须把super放在函数的首位. class Base { Base() { System.out.println("Base" ...

  8. ScrollView嵌套ListView冲突问题的最优解决方式

    项目做多了之后.会发现事实上ScrollView嵌套ListVew或者GridView等非经常常使用,可是你也会发现各种奇怪问题产生.依据个人经验如今列出常见问题以及代码最少最简单的解决方法. 问题一 ...

  9. Mybatis(三):MyBatis缓存详解

    MyBatis缓存分为一级缓存和二级缓存 一级缓存 MyBatis的一级缓存指的是在一个Session域内,session为关闭的时候执行的查询会根据SQL为key被缓存(跟mysql缓存一样,修改任 ...

  10. VS2017调试MVC程序,中文输入法下浏览器闪退,程序调试终止

    工具->选项-> 项目和解决方案->Web项目->浏览器窗口关闭时停止调试器(s) 复选√ 去掉即可,