The Commons HttpClient project is now end of life, and is no longer being developed. It has been replaced by the Apache HttpComponents project in its HttpClient and HttpCore modules, which offer better performance and more flexibility.
commons httpClient在2011年的3.11就结束了;被拆分成了 HttpClient and HttpCore 2个模块;
因此MultiThreadedHttpConnectionManager早就废弃了;网上众多真是误人子弟
最新API为4.5;
2.4. Multithreaded request execution 多线程请求执行;
When equipped with a pooling connection manager such as PoolingClientConnectionManager, HttpClient can be used to execute multiple requests simultaneously using multiple threads of execution.
当配有池连接的管理器如poolingclientconnectionmanager,HttpClient可以用多线程来执行多个请求同时使用。
The PoolingClientConnectionManager will allocate connections based on its configuration. If all connections for a given route have already been leased, a request for a connection will block until a connection is released back to the pool. One can ensure the connection manager does not block indefinitely in the connection request operation by setting 'http.conn-manager.timeout' to a positive value. If the connection request cannot be serviced within the given time period ConnectionPoolTimeoutException will be thrown.
的poolingclientconnectionmanager将分配基于其配置连接。如果一个给定路由的所有连接已经被租用时,新一个连接的请求将被阻止,直到一个连接被释放回池。
如果想让连接管理器不会无限期阻塞的新进来的连接请求,那么给http.conn-manager.timeout' 设置一个值,
如果连接请求不能被服务在给定的时间范围内,connectionpooltimeoutexception异常将被抛出。
While HttpClient instances are thread safe and can be shared between multiple threads of execution, it is highly recommended that each thread maintains its own dedicated instance of HttpContext .
虽然HttpClient实例是线程安全的和可执行的多个线程之间共享,但是HttpContext实例则不是这样,强烈建议每个线程维护自己专用的HttpContext实例。
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm)
.build();
// URIs to perform GETs on
String[] urisToGet = {
"http://www.domain1.com/",
"http://www.domain2.com/",
"http://www.domain3.com/",
"http://www.domain4.com/"
};
// create a thread for each URI
GetThread[] threads = new GetThread[urisToGet.length];
for (int i = 0; i < threads.length; i++) {
HttpGet httpget = new HttpGet(urisToGet[i]);
threads[i] = new GetThread(httpClient, httpget);
}
// start the threads
for (int j = 0; j < threads.length; j++) {
threads[j].start();
}
// join the threads
for (int j = 0; j < threads.length; j++) {
threads[j].join();
}
static class GetThread extends Thread {
private final CloseableHttpClient httpClient;
private final HttpContext context;
private final HttpGet httpget;
public GetThread(CloseableHttpClient httpClient, HttpGet httpget) {
this.httpClient = httpClient;
this.context = HttpClientContext.create();
this.httpget = httpget;
}
@Override
public void run() {
try {
CloseableHttpResponse response = httpClient.execute(
httpget, context);
try {
HttpEntity entity = response.getEntity();
} finally {
response.close();
}
} catch (ClientProtocolException ex) {
// Handle protocol errors
} catch (IOException ex) {
// Handle I/O errors
}
}
}
相关推荐
NULL 博文链接:https://eric-wyf.iteye.com/blog/1179729
这个比xutils强多了。用过服务端交互框架的都知道
4、多线程下载:使用RandomAccessFile输出流写 5、多线程下载-断点续传:使用临时文件记录当前下载的数据,下次读取文件开始下载 6、下载显示进度条-ProgressBar 7、在gitbub上面下载已经有支持断点续传功能的代码...
主要介绍了使用java的HttpClient实现多线程并发的相关资料,需要的朋友可以参考下
NULL 博文链接:https://theoffspring.iteye.com/blog/1610345
httpclient post例子,多线程
NULL 博文链接:https://zywang.iteye.com/blog/916489
本资源中包括新旧版本的HttpClient的jar包,主要包含httpclient-4.5.3.jar、commons-httpclient-3.1.jar、commons-codec-1.9.jar等总计12个jar包文件
用快压解压 Common-httpClient各个版本jar及源码
HttpClient下载数据 图片,string 使用get请求数据
httpclient 老版本依赖的jar包 包含三个文件:commons-codec-1.3.jar,commons-httpclient-3.0.jar,commons-logging-1.0.4.jar,
HttpClient多线程网络下载 经典案例源代码
NULL 博文链接:https://hrsvici412.iteye.com/blog/789460
httpclient-4.5.4.jar最新版本, httpclient-4.5.4.jar最新版本可供使用
简单实现的源码,不会自动判断断点恢复,需要手动注释掉相应代码。 博文链接:https://calmness.iteye.com/blog/220075
HttpClient4.2.1版本,内有所需的该版本jar包
commons-httpclient.rar
HttpClient 4.2.1新版本
Apache HttpClient包有3.0有4.0,此文件名字虽为4.0的