HttpClient4.x之前没有连接池的概念,多少次请求就会建立多少个IO,在访问量巨大的情况下服务器的IO可能会耗尽。最新HttpClient4.2有连接池的东西在里头,使用PoolingClientConnectionManager,具体可以参考官方文档,里面有详细介绍。
关于PoolingClientConnectionManager
介绍原文是这样的:
PoolingClientConnectionManager
is a more complex implementation that manages a pool of client connections and is able to service connection requests from multiple execution threads. Connections are pooled on a per route basis.
A request for a route for which the manager already has a persistent connection available in the pool will be serviced by leasing a connection from the pool rather than creating a brand new connection.
PoolingClientConnectionManager
maintains a maximum limit of connections on a per route basis and in total. Per default this implementation will create no more than 2 concurrent connections per given route and no more 20 connections
in total. For many real-world applications these limits may prove too constraining, especially if they use HTTP as a transport protocol for their services. Connection limits can be adjusted using the appropriate HTTP parameters.
示例:
HttpConnectionManager.java
public class HttpConnectionManager { private static HttpParams httpParams; private static PoolingClientConnectionManager cm; /** * 最大连接数 */ public final static int MAX_TOTAL_CONNECTIONS = 200; /** * 获取连接的最大等待时间 */ public final static int WAIT_TIMEOUT = 60000; /** * 每个路由最大连接数 */ public final static int MAX_ROUTE_CONNECTIONS = 300; /** * 连接超时时间 */ public final static int CONNECT_TIMEOUT = 10000; /** * 读取超时时间 */ public final static int READ_TIMEOUT = 10000; static { SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register( new Scheme("http",80,PlainSocketFactory.getSocketFactory())); schemeRegistry.register( new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); cm = new PoolingClientConnectionManager(schemeRegistry); cm.setMaxTotal(200); cm.setDefaultMaxPerRoute(80); HttpParams params = new BasicHttpParams(); params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,CONNECT_TIMEOUT); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, READ_TIMEOUT); } public static HttpClient getHttpClient() { return new DefaultHttpClient(cm, httpParams); } }
DownloadTask.java
import java.io.IOException; import java.io.InputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.params.CoreConnectionPNames; public class DownloadTask implements Runnable { private HttpClient httpClient = HttpConnectionPoolManager.getHttpClient(); @Override public void run() { String url = "http://www.baidu.com"; download(url); } public void download(String url) { HttpGet httpget = null; InputStream in = null; try { httpget = new HttpGet(url); httpget.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.79 Safari/537.1"); httpget.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); httpget.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,1000 * 20); HttpResponse resp = httpClient.execute(httpget); int respCode = resp.getStatusLine().getStatusCode(); if (respCode == HttpStatus.SC_OK) { HttpEntity entity = resp.getEntity(); if (entity != null) { in = entity.getContent(); sleep(1000); System.out.println("请求成功了..." + Thread.currentThread().getName() + url); } } else { System.out.println("请求失败了..." + Thread.currentThread().getName() + url); if(httpget!=null) httpget.abort(); } } catch (ClientProtocolException e) { e.printStackTrace(); if(httpget!=null) httpget.abort(); } catch (IOException e) { e.printStackTrace(); if(httpget!=null) httpget.abort(); } catch (Exception e) { // 捕获最大的异常 e.printStackTrace(); if(httpget!=null) httpget.abort(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void sleep(int time) throws InterruptedException { Thread.sleep(time); } }
测试类
public class Test { /** * @param args */ public static void main(String[] args) { for(int i=0;i<8;i++){ new Thread(new DownloadTask()).start(); } } }
http://www.xuebuyuan.com/2006070.html
相关推荐
HttpAsyncClient连接池的使用,项目中频繁发送http请求,同步http阻塞主线程,影响性能,使用 HttpAsyncClient可使性能提高,这里配合连接池使用,效果更好,同时还附带同步httpClient的连接池使用
实现httpClient连接池,同时支持http和https请求,代码可直接导入IDE运行,无冗余代码
本demo有两个例子test是作为服务端开启的,httpclient是客户端调用的,模拟服务端的test在启动后是没有界面的,要加上id才能看到json数据,httpclient的HttpClient33.java是测试类,最后的结果是接受到一个对象。...
两个主机建立连接的过程是很复杂的一个过程,涉及到多个数据包...一般情况下,普通使用HttpClient已经能满足我们的需求,不过有时候,在我们需要高并发大量的请求网络的时候,还是用“连接池”这样的概念能提升吞吐量。
基于springboot+httpClient无缝整合在一起的demo例子(资源分享)!
网上关于HttpClient资料很多,但很多都是有代码无jar,也是找了一下午,特供资料,与君互勉。
这是一个android的HttpClient简单demo,实现了post与get两种方式,包括完整的客户端和服务器端程序,数据库采用的是MySql数据库,数据库部分请用户自己完成,都是最简单的,我已经测试通过,这是我一天的成果,参考...
压缩包内包括 httpclient 4.3的jar包和完整的demo程序。想学httpclient 4的赶紧下啊。
HttpClient_Demo HttpClient_Demo
httpclient4.3的demo工程,包含jar包,可直接运行
HttpClient抓取网页Demo,HttpClient 的入门示例,配合blog文章的附件
httpclient Demo 可以看看
本文主要介绍了请求HTPPS的实例.帮助请参考;http://blog.csdn.net/zhangxiaowei_/article/details/39339775
用VC实现http协议,构造httpserver和httpclient两端应用实例。
httpclient Demo 案例 含jar 解决Post传参数字符原样的问题:如:1.0;解决被处理后为‘1’的问题
springboot中注解配置连接池
httpclient和httpcore的jar包
NULL 博文链接:https://bugyun.iteye.com/blog/2395912
利用 android httpclient 编写http协议的例子代码,可以参考添加post,get ,put delete 等功能。工程师文件可以直接编译运行。
HttpClient接口调用工具类 (Post请求 get请求 put请求 delete请求 (附带调用demo) 下载就可以用 ) 可设置httpclient的连接池大小,连接池最大并发连接数,单路由最大并发数设