1.问题背景
一些HTTPS接口提供方有IP白名单策略,接口调用方必须通过报备过的出口IP访问接口。对于部署在云主机上的接口调用方服务而言,这是比较不友好的。
常用的方法是在指定出口IP的服务器上部署正向代理,接口调用方服务通过这个正向代理间接访问HTTPS接口。
2.解决方案
以HttpClient4.5.3为例。
1)定义 HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); |
2)定义代理选择策略 ProxySelector
class MyProxySelector extends ProxySelector { ProxySelector defaultproxySelector = ProxySelector.getDefault(); ArrayList<Proxy> noProxy = new ArrayList<Proxy>(); ArrayList<Proxy> secureProxy = new ArrayList<Proxy>(); public MyProxySelector(String ip, int port) { // 正向代理的IP和端口 noProxy.add(Proxy.NO_PROXY); // 初始化无代理策略 secureProxy.add(new Proxy(Type.HTTP, new InetSocketAddress(ip, port))); // 初始化正向代理策略。PS:正向代理本身一般是HTTP协议的 |