I'm using ThreadSafeClientConnManager
to manage a pool of client connections on HttpClient 4.0.1 environment.
Usually my system works fine when authenticating with NTLM, but sometimes it doesn't work properly.
When authenticating with NTLM, we receive result codes in the same order as below: HTTP/1.1 401 > HTTP/1.1 401 > HTTP/1.1 200. It is correct process.
But, I don't know why, sometimes MS EWS returns 401 result code 3 times: HTTP/1.1 401 > HTTP/1.1 401 > HTTP/1.1 401. After that, EWS only returns HTTP/1.1 200 for every request.
to help you understand better..
- User A: 401 > 401 > 200 (Good)
- User B: 401 > 401 > 200 (Good)
- User C: 401 > 401 > 401 (Issue arises)
- User D: 200
- User E: 200
- User F: 200
So, when the issue arises, UserC,D,E,F... can use our system properly.
Here are my questions
- Why does EWS only return 200 code after returning 401 three times?
- How can I abort the connection when I keep receiving the result code, HTTP/1.1 401?
In more detail,
Why does EWS only return 200 code after returning 401 three times? It doesn't happen often, though. The username and password for authentication is correct.
How can I abort the connection when I receive the third HTTP/1.1 401? I use
request.abort()
, but it doesn't work.
My code is as below.
private Response processRequest(AbstractHttpClient httpClient, HttpRequestBase httpRequest, HttpContext httpContext){
Response response = new Response();
try {
// execute request
HttpResponse httpResponse = null;
if(httpContext != null){
httpResponse = httpClient.execute(httpRequest, httpContext);
// retrieve cookie
CookieStore cookieStore = (CookieStore)httpContext.getAttribute(ClientContext.COOKIE_STORE);
List<Cookie> cookieList = cookieStore.getCookies();
for(Cookie cookie : cookieList){
com.XXX.mo.connectivity.info.http.Cookie newCookie = new com.XXX.mo.connectivity.info.http.Cookie(cookie.getName(), cookie.getValue());
newCookie.setVersion(cookie.getVersion());
newCookie.setDomain(cookie.getDomain());
newCookie.setPath(cookie.getPath());
response.addCookie(newCookie);
}
}else{
httpResponse = httpClient.execute(httpRequest);
}
// retrieve header
response.setStatusCode(httpResponse.getStatusLine().getStatusCode());
Header [] headers = httpResponse.getAllHeaders();
for(Header header : headers){
response.addHeader(new com.XXX.mo.connectivity.info.http.Header(header.getName(), header.getValue()));
}
// retrieve body
HttpEntity httpEntity = httpResponse.getEntity();
if(httpEntity != null){
response.setContentCharSet(EntityUtils.getContentCharSet(httpEntity));
response.setBody(EntityUtils.toByteArray(httpEntity));
httpEntity.consumeContent();
}
} catch (Exception e) {
e.printStackTrace();
httpRequest.abort();
}
return response;
}