httpclient妙用一 httpclient作为客户端调用webservice | http://aperise.iteye.com/blog/2223454 |
httpclient妙用二 httpclient保持会话登录 | http://aperise.iteye.com/blog/2223470 |
httpclient连接池 | http://aperise.iteye.com/blog/2295153 |
httpclient连接池
1.连接池原理(待续)
2.httpclient连接池如何保证连接交回至连接池管理器
2.1 方式一
HttpResponse response = (httpMethod); HttpEntity entity = response.getEntity(); //这两段代码返回的entity是HttpEntity的实现类BasicManagedEntity。此时与本次请求关联的连接尚未归还至连接管理器。需要调用以下两条语句: InputStream instream = entity.getContent();//获得响应具体内容 //处理响应:代码省略 instream.close();//关闭输入流同时会将连接交回至连接处理器
2.2 方式二使用默认的响应处理器BasicResponseHandler
httpClient Jar包中提供BasicResponseHandler。如果返回的类型能确定需要解码为String类型的话,推荐使用该响应处理器。 该处理器解码http连接响应字节流为String类型,对返回码>=300的响应进行了异常封装,并能够保证连接交还给连接池管理器。 该处理器将字节解码为字符的过程依次如下: 1)如果响应http报文Head部分由指定的charset,则使用该charset进行解码,否则进行下一步。例如使用UTF-8解码以下响应:DEBUG org.apache.http.headers - $amp; 2)如果响应报文未执行charset,则使用传入EntityUntils.toString()时指定的charset进行解码。否则进行下一步 3)使用ISO-8859-1进行解码。
3.实现连接池(待续)
4.遇到的问题
4.1 连接池阻塞,频繁的报以下错误信息
pooling post() method executing IOException:java.net.SocketTimeoutException: Read timed out java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.read(SocketInputStream.java:129) at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:136) at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:152) at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:270) at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:140) at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57) at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260) at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:161) at sun.reflect.GeneratedMethodAccessor96.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.http.impl.conn.CPoolProxy.invoke(CPoolProxy.java:138) at com.sun.proxy.$Proxy43.receiveResponseHeader(Unknown Source) at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271) at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123) at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:254) at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195) at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:85) at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:214) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:160) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:136)
上述问题主要问题是连接池中httpclient连接在遇到异常情况下未正常关闭导致,出问题时候连接池关于回收httpclient连接的代码如下:
private ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= HttpStatus.SC_OK && status < HttpStatus.SC_MULTIPLE_CHOICES) { HttpEntity entity = response.getEntity(); Charset charset = ContentType.getOrDefault(entity).getCharset(); if (charset == null) { charset = Charset.forName("UTF-8"); } return entity != null ? EntityUtils.toString(entity, charset) : null; } else { //注意这里没有告诉连接池要回收连接 throw new ClientProtocolException("Unexpected response status: " + status); } };
修改后的代码如下:
private ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { final StatusLine statusLine = response.getStatusLine(); final HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= HttpStatus.SC_MULTIPLE_CHOICES||statusLine.getStatusCode()<HttpStatus.SC_OK) { //主动回收httpclient连接 EntityUtils.consume(entity); throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } Charset charset = ContentType.getOrDefault(entity).getCharset(); if (charset == null) { charset = Charset.forName("UTF-8"); } return entity != null ? EntityUtils.toString(entity, charset) : null; } };
相关推荐
HttpAsyncClient连接池的使用,项目中频繁发送http请求,同步http阻塞主线程,影响性能,使用 HttpAsyncClient可使性能提高,这里配合连接池使用,效果更好,同时还附带同步httpClient的连接池使用
众所周知,httpclient是java开发中非常常见的一种访问网络资源的方式了,下面这篇文章主要给大家介绍了关于Http持久连接和HttpClient连接池的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,...
springboot中注解配置连接池
NULL 博文链接:https://liujunhg.iteye.com/blog/1137411
封装好的httpClient工具类里面包含了get 和 post两种请求
实现httpClient连接池,同时支持http和https请求,代码可直接导入IDE运行,无冗余代码
Httpclient连接池
两个主机建立连接的过程是很复杂的一个过程,涉及到多个数据包...一般情况下,普通使用HttpClient已经能满足我们的需求,不过有时候,在我们需要高并发大量的请求网络的时候,还是用“连接池”这样的概念能提升吞吐量。
HttpClient连接池 Spring依赖注入 lombok简化POJO开发 原子指标 内置锁 竣工服务 log4j+slf4j日志 实现的功能 登录注销 单聊 群聊 客户端提交任务,下载图片并显示 上线下线公告 在线用户记录 批量下载豆瓣电影的...
主要介绍了使用java的HttpClient实现多线程并发的相关资料,需要的朋友可以参考下
- HttpClient连接池 - Spring依赖注入 - lombok简化POJO开发 - 原子变量 - 内置锁 - CompletionService - log4j+slf4j日志 - 实现的功能 - 登录注销 - 单聊 - 群聊 - 客户端提交任务,下载图片并显示 -...
12.2.2 HttpClient连接池源码分析 240 12.2.3 HttpClient 4.2.3配置 241 12.2.4 问题示例 243 12.3 线程池 244 12.3.1 Java线程池 245 12.3.2 Tomcat线程池配置 248 13 异步并发实战 250 13.1 同步阻塞调用 251 13.2...
HttpClientFactory(同步异步httpclient连接池工厂) http.sync HttpSyncClient(httpclient4.5.x封装) HttpSSLConnectionSocketFactory(https请求实现) HttpSSLConnectionSocketFactoryV2(https协议增加) ...
http连接池官方说明文档
Java基于多线程和NIO实现聊天室涉及到的技术点线程池ThreadPoolExecutor阻塞队列BlockingQueue,生产者消费者模式SelectorChannelByteBufferProtoStuff 高性能序列化HttpClient连接池Spring依赖注入lombok简化POJO...
连接池整合、跨域资源请求处理等。 项目结构 zhuozun ├ ├── zhuozun-admin-server -- 服务监控 | ├── zhuozun-api -- api接口,提供feign接口 | ├── zhuozun-cache-api -- 缓存接口api,提供feign接口 | ...
HttpClient接口调用工具类 (Post请求 get请求 put请求 delete请求 (附带调用demo) 下载就可以用 ) 可设置httpclient的连接池大小,连接池最大并发连接数,单路由最大并发数设
pool - 一个具有自主连接池和速率限制的HTTP客户端
httpclient获取目标网站内容,get、post方式(可运行),可以通过配置参数、配置连接池实现
没有使用框架,httpclient连接池+线程池+正则实现. 厚颜无耻的挂上博客+求波星 :) [removed]alert("XSS")[removed] [removed]alert(123)[removed] ´úÂë¹ýÂË£º medium--> ˫дÈÆ...