Java 如何中止HttpRequest?
我正在使用Java 如何中止HttpRequest?,java,httpclient,exchangewebservices,abort,Java,Httpclient,Exchangewebservices,Abort,我正在使用ThreadSafeClientConnManager管理HttpClient 4.0.1环境上的客户端连接池。 通常我的系统在使用NTLM进行身份验证时可以正常工作,但有时不能正常工作 当使用NTLM进行身份验证时,我们将按以下相同顺序接收结果代码:HTTP/1.1 401>HTTP/1.1 401>HTTP/1.1 200。 这是正确的过程 但是,我不知道为什么,有时候MS EWS会返回401结果代码3次:HTTP/1.1 401>HTTP/1.1 401>HTTP/1.1 401
ThreadSafeClientConnManager
管理HttpClient 4.0.1环境上的客户端连接池。
通常我的系统在使用NTLM进行身份验证时可以正常工作,但有时不能正常工作
当使用NTLM进行身份验证时,我们将按以下相同顺序接收结果代码:HTTP/1.1 401>HTTP/1.1 401>HTTP/1.1 200。
这是正确的过程
但是,我不知道为什么,有时候MS EWS会返回401结果代码3次:HTTP/1.1 401>HTTP/1.1 401>HTTP/1.1 401。
之后,EWS只为每个请求返回HTTP/1.1200
为了帮助你更好地理解
- 用户A:401>401>200(良好)
- 用户B:401>401>200(良好)
- 用户C:401>401>401(出现问题)
- 用户D:200
- 用户E:200
- 用户F:200
request.abort()
,但它不起作用 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;
}
私有响应processRequest(AbstractHttpClient httpClient,HttpRequestBase httpRequest,HttpContext HttpContext){
响应=新响应();
试一试{
//执行请求
HttpResponse HttpResponse=null;
if(httpContext!=null){
httpResponse=httpClient.execute(httpRequest,httpContext);
//检索cookie
CookieStore CookieStore=(CookieStore)httpContext.getAttribute(ClientContext.COOKIE_存储);
List cookieList=cookieStore.getCookies();
用于(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());
addCookie(newCookie);
}
}否则{
httpResponse=httpClient.execute(httpRequest);
}
//检索标题
response.setStatusCode(httpResponse.getStatusLine().getStatusCode());
Header[]headers=httpResponse.getAllHeaders();
用于(标题:标题){
response.addHeader(新的com.XXX.mo.connectivity.info.http.Header(Header.getName(),Header.getValue());
}
//取回尸体
HttpEntity HttpEntity=httpResponse.getEntity();
if(httpEntity!=null){
response.setContentCharSet(EntityUtils.getContentCharSet(httpEntity));
response.setBody(EntityUtils.toByteArray(httpEntity));
httpEntity.consumerContent();
}
}捕获(例外e){
e、 printStackTrace();
httpRequest.abort();
}
返回响应;
}