Reputation: 139
Can someone point me to authoritative example that demonstrates use of Proxy with authentication.
My searches reveal varied examples not necessarily using ver. 4.3.6 and hence the confusion.
I encountered two approaches as following. I prefer NOT to set the proxy and credentials for every request and hence wondering whats the best practice here? Also I have to make sure this works for Basic, Digest and NTLM schemes.
Approach 1:
// 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);
Approach 2:
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 ?
I am sure to get some heckling for this one but so far I have wondered too much hence asking some help. thanks,
Upvotes: 2
Views: 2859
Reputation: 139
Ok Here is what I ended up doing and solves the use case.
// 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();
}
Now in other places use this client to perform HTTP operations.
// 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(...) { ... }
This helps to create a HTTP client with all desired characteristics that can be re-used for various HTTP operations in different places.
Upvotes: 2