Httpclient 4. X Auth Cache Not Set in the Context
I Am Using Httpclient 4.5. X to Call a Soap Web Service with Ntlm Authentication. Authentication Happens Successfully. It Is a 3 Way Handshake. for Example, If...
I am using HttpClient 4.5.x to call a SOAP web service with NTLM authentication. Authentication happens successfully. It is a 3 way handshake. For example, if i do post-request including images or other data content, then for each handshake request, the data is sent.
One recommendation from the HttpClients material online is to do a cheap request first, and use the same client context object for the subsequent big size request. It also says this in the documentation - As of version 4.1, HttpClient automatically caches information about hosts it has successfully authenticated.
I tried the same. I have both these requests subsequently happening in the same method, and the same thread. The (default) caching does not happen. Both times the 3-way handshake was happening.
In the log I see the following statement
[org.apache.http.client.protocol.RequestAuthCache] Auth cache not set in the context
May be this default does not work for NTLM. Is there any flag to turn the caching on? Or should I create AuthCache myself and maintain? It looks like only for preemptive authentication, one creates auth cache. So, I am doubtful if it applies to my case.
private void callServiceWithAuthentication (ByteArrayEntity entity)
{
try {
/*
AuthScope authScope = new AuthScope("WEBSERVICE-HOST", 443, AuthScope.ANY_REALM, "ntlm");
*/
CloseableHttpClient httpclient = HttpClients.createDefault();
NTCredentials ntCredentials = new NTCredentials(this.userName, this.password, null, "DOMAIN");
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, ntCredentials);
httpClientContext = HttpClientContext.create();
httpClientContext.setCredentialsProvider(credentialsProvider);
HttpHost targetHost = new HttpHost ("WEBSERVICE_HOST", 443);
// First cheap-cost request
HttpGet httpGet = new HttpGet ("WEBSERVICE_URL");
CloseableHttpResponse httpResponse = httpclient.execute(httpGet, httpClientContext);
HttpEntity getResponseEntity = httpResponse.getEntity();
// Following real costly post request
HttpPost post = new HttpPost("WEBSERVICE_URL");
post.setEntity(entity);
// Execute request
try {
CloseableHttpResponse response = httpclient.execute(post, httpClientContext);
StatusLine statusLine = response.getStatusLine();
HttpEntity rentity = response.getEntity();
System.out.println ("HTTP Response Code: " + statusLine.getStatusCode());
if (statusLine.getStatusCode() >= 300) {
throw new HttpResponseException(
statusLine.getStatusCode(),
statusLine.getReasonPhrase());
}
if (entity == null) {
throw new ClientProtocolException("Response contains no content");
}
System.out.println (rentity.getContent());
}
catch (Exception e) {
e.printStackTrace();
}
finally {
// Release current connection to the connection pool once you are done
post.releaseConnection();
}
}
catch (Exception e) {e.printStackTrace();}
finally {
}
}
1 Answer
I had to have the first response entity consume before the second request. Then the second request did not do the (3-way handshake) authentication again.
EntityUtils.consume(getResponseEntity);
I have more cleanup to do