【问题标题】:Using authenticated proxy with apache http client 4.3.6将经过身份验证的代理与 apache http 客户端 4.3.6 一起使用
【发布时间】:2015-03-12 03:24:49
【问题描述】:

有人能指出权威的例子,演示使用代理和身份验证。

我的搜索揭示了不一定使用版本的各种示例。 4.3.6 和因此的混乱。

我遇到了以下两种方法。我不想为每个请求设置代理和凭据,因此想知道这里的最佳实践是什么?我还必须确保这适用于 Basic、Digest 和 NTLM 方案。

方法一:

// Create client and set credential provider
CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider).build();
// Every request is set the with proxy settings
RequestConfig config = RequestConfig.custom()
            .setProxy(proxy)
            .build();
HttpGet httpget = new HttpGet("/");
httpget.setConfig(config); 

方法二:

HttpHost proxyHost = new HttpHost(proxyServer, proxyPort);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyHost);

HttpClientBuilder clientBuilder = HttpClients.custom()
            .setConnectionManager(connectionManager);
clientBuilder.setRoutePlanner(routePlanner);

client = clientBuilder.build();

// Where and how to set credentials as best practice ? 

我肯定会为此受到一些质疑,但到目前为止,我想知道太多,因此寻求帮助。 谢谢,

【问题讨论】:

    标签: apache http authentication proxy


    【解决方案1】:

    好的,这就是我最终要做的并解决了用例。

    // Method that returns HTTP Client that can be re-used for various GET/POST/... calls
    static CloseableHttpClient makeHttpClient(...) throws IOException {
    
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        // Default MAX connections per route
        connectionManager.setDefaultMaxPerRoute(...);
        // MAX total connections
        connectionManager.setMaxTotal(...);
    
        HttpClientBuilder clientBuilder = HttpClients.custom()
                .setConnectionManager(connectionManager);
        // Set proxy if needed
        HttpHost proxyHost = new HttpHost(theProxyServer, port);
       DefaultProxyRoutePlanner routePlanner = new  DefaultProxyRoutePlanner(proxyHost);
       clientBuilder.setRoutePlanner(routePlanner);
    
       // Select and configure the properties you are interested in
       RequestConfig config = RequestConfig.custom()
                        .setProxy(proxyHost)
                        .setRedirectsEnabled(true)
                        .setMaxRedirects(5)
                        .setConnectTimeout(100 * 1000)
                        .setConnectionRequestTimeout(300 * 1000)
                        .setSocketTimeout(300 * 1000)
                        .build();
    
       // set proxy authentication as needed
       CredentialsProvider credentialProvider = new BasicCredentialsProvider();
       // set NTLM/basic/digest credentials
       credentialProvider.setCredentials(...)
       ....
    
       clientBuilder.setDefaultCredentialsProvider(credentialProvider);
       clientBuilder.setDefaultRequestConfig(config);
    
       // Now build and return the client
       return clientBuilder.build();
    }
    

    现在在其他地方使用这个客户端来执行 HTTP 操作。

    // get HTTP client
    makeHttpClient(...)
    // get payload 
    HttpPost postMethod = new HttpPost(url);
    postMethod.setHeader(CONTENT_TYPE, CONTENT_TYPE_JSON);
    postMethod.setEntity(payload);
    
    try {
            response = client.execute(postMethod);
            ...
    } catch(...) { ... }
    

    这有助于创建一个具有所有所需特征的 HTTP 客户端,这些特征可以在不同的地方重复用于各种 HTTP 操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-11
      • 2013-03-28
      相关资源
      最近更新 更多