Reputation: 65
I am trying to use httpclient5 with connection pooling. Below are some of the points mentioned in documentation:
CloseableHttpClient
instances should be closed when no longer needed or about to go out of score.IMPORTANT: Always re-use
CloseableHttpClient
instances. They are expensive to create, but they are also fully thread safe, so multiple threads can use the same instance ofCloseableHttpClient
to execute multiple requests concurrently taking full advantage of persistent connection re-use and connection pooling.
I feel both of them feels contradictory. How should I proceed with this?
Also in case if I am reusing CloseableHttpClient
instances how can I change credentials for different requests which uses same client?
Upvotes: 2
Views: 1041
Reputation: 2357
depends on your auth type... for e.g. basic auth just set the header manually for each request
final HttpGet request = new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION);
final String auth = DEFAULT_USER + ":" + DEFAULT_PASS;
final byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1));
final String authHeader = "Basic " + new String(encodedAuth);
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
https://www.baeldung.com/httpclient-basic-authentication
Upvotes: 0