使用带有HttpComponentsClientHttpRequestFactory和RestTemplate的Proxy

时间:2015-12-16 18:40:02

标签: spring resttemplate apache-httpcomponents proxyselector

有人可以指导我如何配置HttpComponentsClientHttpRequestFactory以使用代理服务器。

我见过的所有示例都使用SimpleClientHttpRequestFactory

2 个答案:

答案 0 :(得分:16)

如果你不介意使用Apache Http Client,它不是很复杂,有两种可能性:

  1. 如果所有目标的单个代理对您来说足够了:

    HttpComponentsClientHttpRequestFactory clientHttpRequestFactory 
        = new HttpComponentsClientHttpRequestFactory(
            HttpClientBuilder.create()
                    .setProxy(new HttpHost("myproxy.com", 80, "http"))
                    .build());
    restTemplate = new RestTemplate(clientHttpRequestFactory);
    
  2. 或者,如果您要为不同的目标URI,模式等使用不同的代理,则可以将HttpRoutePlanner与自定义ProxySelector一起使用:

    HttpRoutePlanner routePlanner = new SystemDefaultRoutePlanner(new MyProxySelector());
    
    HttpComponentsClientHttpRequestFactory clientHttpRequestFactory 
        = new HttpComponentsClientHttpRequestFactory(
            HttpClientBuilder.create()
                .setRoutePlanner(routePlanner)
                .build());
    restTemplate = new RestTemplate(clientHttpRequestFactory);
    

    代理选择器示例: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
        }
    }
    

答案 1 :(得分:0)

让HttpComponentsClientHttpRequestFactory使用标准java SystemProperties代理的非常简单的方法(参见https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html

就是这样:

        HttpRoutePlanner routePlanner = new SystemDefaultRoutePlanner(ProxySelector.getDefault());

    HttpClient httpClient = HttpClientBuilder
            .create()
            .setRoutePlanner(routePlanner)
            .build();
    restTemplate.setRequestFactory(
            new HttpComponentsClientHttpRequestFactory(httpClient));

这样,它甚至会考虑nonProxyHosts设置。