httpclient保持会话登录

1.连接池原理(待续)

2.httpclient连接池如何保证连接交回至连接池管理器

    2.1 方式一

Java代码   收藏代码
  1. HttpResponse response = (httpMethod);  
  2. HttpEntity entity = response.getEntity();  
  3. //这两段代码返回的entity是HttpEntity的实现类BasicManagedEntity。此时与本次请求关联的连接尚未归还至连接管理器。需要调用以下两条语句:  
  4. InputStream instream = entity.getContent();//获得响应具体内容  
  5. //处理响应:代码省略  
  6. instream.close();//关闭输入流同时会将连接交回至连接处理器  

     2.2 方式二使用默认的响应处理器BasicResponseHandler

Java代码   收藏代码
  1. httpClient Jar包中提供BasicResponseHandler。如果返回的类型能确定需要解码为String类型的话,推荐使用该响应处理器。  
  2. 该处理器解码http连接响应字节流为String类型,对返回码>=300的响应进行了异常封装,并能够保证连接交还给连接池管理器。  
  3. 该处理器将字节解码为字符的过程依次如下:  
  4. 1)如果响应http报文Head部分由指定的charset,则使用该charset进行解码,否则进行下一步。例如使用UTF-8解码以下响应:DEBUG org.apache.http.headers - $amp;  
  5. 2)如果响应报文未执行charset,则使用传入EntityUntils.toString()时指定的charset进行解码。否则进行下一步  
  6. 3)使用ISO-8859-1进行解码。  

3.实现连接池(待续)

4.遇到的问题

    4.1 连接池阻塞,频繁的报以下错误信息

Java代码   收藏代码
  1. pooling post() method executing IOException:java.net.SocketTimeoutException: Read timed out  
  2. java.net.SocketTimeoutException: Read timed out  
  3.         at java.net.SocketInputStream.socketRead0(Native Method)  
  4.         at java.net.SocketInputStream.read(SocketInputStream.java:129)  
  5.         at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:136)  
  6.         at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:152)  
  7.         at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:270)  
  8.         at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:140)  
  9.         at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)  
  10.         at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260)  
  11.         at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:161)  
  12.         at sun.reflect.GeneratedMethodAccessor96.invoke(Unknown Source)  
  13.         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)  
  14.         at java.lang.reflect.Method.invoke(Method.java:597)  
  15.         at org.apache.http.impl.conn.CPoolProxy.invoke(CPoolProxy.java:138)  
  16.         at com.sun.proxy.$Proxy43.receiveResponseHeader(Unknown Source)  
  17.         at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)  
  18.         at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)  
  19.         at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:254)  
  20.         at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)  
  21.         at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:85)  
  22.         at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)  
  23.         at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)  
  24.         at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)  
  25.         at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:214)  
  26.         at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:160)  
  27.         at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:136)  

 上述问题主要问题是连接池中httpclient连接在遇到异常情况下未正常关闭导致,出问题时候连接池关于回收httpclient连接的代码如下:

Java代码   收藏代码
  1. private ResponseHandler<String> responseHandler = new ResponseHandler<String>() {  
  2.     public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {  
  3.         int status = response.getStatusLine().getStatusCode();  
  4.         if (status >= HttpStatus.SC_OK && status < HttpStatus.SC_MULTIPLE_CHOICES) {  
  5.             HttpEntity entity = response.getEntity();  
  6.               
  7.             Charset charset = ContentType.getOrDefault(entity).getCharset();  
  8.             if (charset == null) {  
  9.                 charset = Charset.forName("UTF-8");  
  10.             }  
  11.             return entity != null ? EntityUtils.toString(entity, charset) : null;  
  12.         } else {  
  13.                             //注意这里没有告诉连接池要回收连接  
  14.             throw new ClientProtocolException("Unexpected response status: " + status);  
  15.         }  
  16. };  

 修改后的代码如下:

Java代码   收藏代码
  1. private ResponseHandler<String> responseHandler = new ResponseHandler<String>() {  
  2.     public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {  
  3.   
  4.         final StatusLine statusLine = response.getStatusLine();  
  5.         final HttpEntity entity = response.getEntity();  
  6.         if (statusLine.getStatusCode() >= HttpStatus.SC_MULTIPLE_CHOICES||statusLine.getStatusCode()<HttpStatus.SC_OK) {  
  7.             //主动回收httpclient连接  
  8.         EntityUtils.consume(entity);  
  9.             throw new HttpResponseException(statusLine.getStatusCode(),  
  10.                     statusLine.getReasonPhrase());  
  11.         }  
  12.         Charset charset = ContentType.getOrDefault(entity).getCharset();  
  13.         if (charset == null) {  
  14.             charset = Charset.forName("UTF-8");  
  15.         }  
  16.         return entity != null ? EntityUtils.toString(entity, charset) : null;  
  17.     }  
  18. };  

猜你喜欢

转载自javatea.iteye.com/blog/2384241