httpclient设置proxy与proxyselector
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);Or if you want to use different proxies for different target URIs, schemas, etc. you can use
HttpRoutePlannerwith customProxySelector:HttpRoutePlanner routePlanner = new SystemDefaultRoutePlanner(new MyProxySelector()); HttpComponentsClientHttpRequestFactory clientHttpRequestFactory
= new HttpComponentsClientHttpRequestFactory(
HttpClientBuilder.create()
.setRoutePlanner(routePlanner)
.build());
restTemplate = new RestTemplate(clientHttpRequestFactory);- Example proxy selector:
MyProxySelector.java: 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的更多相关文章
- 通过httpClient设置代理Ip
背景: 我们有个车管系统,需要定期的去查询车辆的违章,之前一直是调第三方接口去查,后面发现数据不准确(和深圳交警查的对不上),问题比较多.于是想干脆直接从深圳交警上查,那不就不会出问题了吗,但是问题又 ...
- HttpClient 设置代理方式
HttpClient httpClient = new HttpClient(); //设置代理服务器的ip地址和端口 httpClient.getHostConfiguration().setPro ...
- httpclient: 设置请求的超时时间,连接超时时间等
httpclient: 设置请求的超时时间,连接超时时间等 public static void main(String[] args) throws Exception{ //创建httpclien ...
- HttpClient设置代理,超时,以及得到cookies
import java.net.URI; import java.util.List; import org.apache.http.HttpEntity; import org.apache.htt ...
- 实现代理设置proxy
用户在哪些情况下是需要设置网络代理呢? 1. 内网上不了外网,需要连接能上外网的内网电脑做代理,就能上外网:多个电脑共享上外网,就要用代理: 2.有些网页被封,通过国外的代理就能看到这被封的网站:3. ...
- Django如何设置proxy
设置porxy的原因 一般情况下我们代理设置是针对与浏览器而言,通常只需在浏览器设置中进行配置,但它只针对浏览器有效,对我们自己编写的程序并任何效果,这时就需要我们在软件编码中加入代理设置. --- ...
- 设置Proxy Server和SQL Server实现互联网上的数据库安全
◆首先,我们需要了解一下SQL Server在WinSock上定义协议的步骤: 1. 在”启动”菜单上,指向”程序/Microsoft Proxy Server”,然后点击”Microsoft Man ...
- C# HttpClient设置cookies的两种办法 (转发)
一般有两种办法 第一种handler.UseCookies=true(默认为true),默认的会自己带上cookies,例如 var handler = new HttpClientHandler() ...
- selenium设置proxy、headers(phantomjs、Chrome、Firefox)
phantomjs 设置ip 方法1: service_args = [ '--proxy=%s' % ip_html, # 代理 IP:prot (eg:192.168.0.28:808) '--p ...
随机推荐
- 表单提交时上传图片 表单ajax提交
页面 <script type="text/javascript" src="js/jquery.form.js"></script>& ...
- vue scoped 解决样式不生效问题
对于添加样式不能影响子组件样式的情况使用:>>> <style scoped> .sti-inline{ display: inline; } .sti-searchfo ...
- RAID详解[RAID0/RAID1/RAID10/RAID5] (转)
一.RAID定义RAID(Redundant Array of Independent Disk 独立冗余磁盘阵列)技术是加州大学伯克利分校1987年提出,最初是为了组合小的廉价磁盘来代替大的昂贵磁盘 ...
- javascript 清空数组的方法
1,splice var ary = [1,2,3,4]; ary.splice(0,ary.length); console.log(ary); // 输出 [],空数组,即被清空了 2,lengt ...
- Oracle 存储过程调用返回游标的另一个存储过程。
一个扩展存储过程调用另一个存储过程,示例: 被调用存储过程:最后会返回一个游标,游标返回一个值.调用这个存储过程的存储过程同样需要获取它. procedure SearchBill --根据到货单号查 ...
- 一个请求在Struts2框架中的处理的步骤
- sqlserver用户角色相关的权限
- Atitit.atiDataStoreService v2 新特性
Atitit.atiDataStoreService v2 新特性 1.1. V1 基础实现1 1.2. V2 增加了对 $uuid $cur_uid参数的支持1 1.3. 增加了fld ...
- dos指令 批处理文件
windows下开发的时候难免写一些脚本,脚本的调用又难以避免的写批处理文件,也就是(.bat)文件!这个文件是什么呢?其实就是以下的这些dos命令.以下是从网上摘抄的,留以记录,待以后需要时查阅.也 ...
- Hybird App ( 混合模式移动应用)开发初体验
最近1,2个月一直都尝试开发一款Hybird app,遇到了很多问题,谈谈自己的体会. Hybird app (混合模式移动应用),它利用例如安卓端webview组件+HTML5内嵌的方式混合的方式开 ...