博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
httpclient设置proxy与proxyselector
阅读量:4687 次
发布时间:2019-06-09

本文共 1988 字,大约阅读时间需要 6 分钟。

  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
    noProxy = new ArrayList
    (); ArrayList
    secureProxy = new ArrayList
    (); ArrayList
    sociaMediaProxy = new ArrayList
    (); 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
    select(URI uri) { // No proxy for local company addresses. if ( uri.getHost().toLowerCase().endsWith("mycompany.com") ) { return noProxy ; } // Special proxy for social networks. String

转载于:https://www.cnblogs.com/zhengqun/p/8302702.html

你可能感兴趣的文章
memcached 细究(三)
查看>>
future
查看>>
关于main函数传参数的问题
查看>>
getTickCount()函数 VS GetTickCount()函数
查看>>
嵌入式jetty
查看>>
2017~回顾分享
查看>>
let const var的区别与作用
查看>>
计算出线在屏幕内的最长坐标
查看>>
使用svn——项目的目录布局
查看>>
Linux学习之CentOS(二十五)--Linux磁盘管理:LVM逻辑卷基本概念及LVM的工作原理
查看>>
【bzoj4310/hdu5030-跳蚤】后缀数组
查看>>
深度信任网络的快速学习算法(Hinton的论文)
查看>>
RSA System.Security.Cryptography.CryptographicException
查看>>
s的封装和信息隐蔽
查看>>
excelhttp://www.cnblogs.com/caoyuanzhanlang/p/3591904.html
查看>>
webservice整合spring cxf
查看>>
[解题报告] 100 - The 3n + 1 problem
查看>>
Entity Framework 学习高级篇1—改善EF代码的方法(上)
查看>>
Mybatis逆向工程配置文件详细介绍(转)
查看>>
String类的深入学习与理解
查看>>