gpt4 book ai didi

spring - 将代理与 HttpComponentsClientHttpRequestFactory 和 RestTemplate 一起使用

转载 作者:IT老高 更新时间:2023-10-28 13:48:29 35 4
gpt4 key购买 nike

谁能指导我如何配置 HttpComponentsClientHttpRequestFactory 以使用代理服务器。

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

最佳答案

如果您不介意使用 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
    }
    }

关于spring - 将代理与 HttpComponentsClientHttpRequestFactory 和 RestTemplate 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34319679/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com