将代理与HttpComponentsClientHttpRequestFactory和RestTemplate一起使用 [英] Using Proxy with HttpComponentsClientHttpRequestFactory and RestTemplate

查看:2043
本文介绍了将代理与HttpComponentsClientHttpRequestFactory和RestTemplate一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can some one guide me how can I configure HttpComponentsClientHttpRequestFactory to use proxy server.

我看到的所有示例都使用SimpleClientHttpRequestFactory.

All examples I have seen are using SimpleClientHttpRequestFactory.

推荐答案

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

If you do not mind using Apache Http Client it is not very complicated and there are 2 possibilities:

  1. 如果所有目标的单个代理足以满足您的需求:

  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);

  • 或者如果您想对不同的目标URI,模式等使用不同的代理,则可以将HttpRoutePlanner与自定义ProxySelector一起使用:

  • Or if you want to use different proxies for different target URIs, schemas, etc. you can use HttpRoutePlanner with custom 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
        }
    }
    

  • 这篇关于将代理与HttpComponentsClientHttpRequestFactory和RestTemplate一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆