当前位置:  开发笔记 > 运维 > 正文

使用带有HttpComponentsClientHttpRequestFactory和RestTemplate的Proxy

如何解决《使用带有HttpComponentsClientHttpRequestFactory和RestTemplate的Proxy》经验,为你挑选了1个好方法。

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

我见过的所有例子都在使用SimpleClientHttpRequestFactory.



1> Michal Foksa..:

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

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

    HttpComponentsClientHttpRequestFactory clientHttpRequestFactory 
        = new HttpComponentsClientHttpRequestFactory(
            HttpClientBuilder.create()
                    .setProxy(new HttpHost("myproxy.com", 80, "http"))
                    .build());
    restTemplate = new RestTemplate(clientHttpRequestFactory);
    

    或者,如果要对不同的目标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 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 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
        }
    }
    

推荐阅读
个性2402852463
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有