Coin163

首页 > Httpclient多线程访问

Httpclient多线程访问

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

相关推荐:Cookies,SSL,httpclient的多线程处理,HTTP方法

HttpClient能自动管理cookie,包括允许服务器设置cookie并在需要的时候自动将cookie返回服务器,它也支持手工设置cookie后发送到服务器端。不幸的是,对如何处理cookie,有几个规范互相冲突:Netscape Cookie 草案, RFC2109, RFC2965,而且还有很大数量的软件

如果要httpclient模拟一个浏览器反复访问某一组页面,那么多线程是一个非常好的选择,超过一个以上的线程必须要用连接管理,主要工具类是ThreadSafeClientConnManager。 下面是示例代码:

package test.ffm83.commons.httpClient;

import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.time.StopWatch; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils;

/**

* httpClient 的多线程处理。利用ThreadSafeClientConnManager进行管理超过一个以上的线程必须要用连接管理基于4.x版本

*

* @author范芳铭

*/ public classEasyExecuteMultiThreads {

public static void main(String[] args) throws Exception {

StopWatchsw = newStopWatch();

sw.start();

runMutiThread();

sw.stop();

System.out.println("runMutiThread used "+ sw.getTime() + " ms.");

}

private static void runMutiThread() throws Exception {

PoolingHttpClientConnectionManagercm = newPoolingHttpClientConnectionManager();

cm.setMaxTotal(100);

CloseableHttpClienthttpclient = HttpClients.custom()

.setConnectionManager(cm).build();

try {

String[]urisToGet = {

"http://blog.csdn.net/ffm83/article/details/41944921",

"http://blog.csdn.net/ffm83/article/details/41848149",

"http://blog.csdn.net/ffm83/article/details/41892759", };

// 创建线程组

GetThread[]threads = newGetThread[urisToGet.length];

for (int i = 0; i < threads.length; i++) {

HttpGethttpget = newHttpGet(urisToGet[i]);

threads[i]= newGetThread(httpclient, httpget, i + 1);

}

// 启动线程

for (int j = 0; j < threads.length; j++) {

threads[j].start();

}

// 等待线程结束

for (int j = 0; j < threads

相关推荐:轻松把玩HttpClient之封装HttpClient工具类(四),单线程调用及多线程批量调用测试

转载地址:http://blog.csdn.net/xiaoxian8023/article/details/49910885 本文主要来分享一下该工具类的测试结果。工具类的整体源码不再单独分享,源码基本上都已经在文章中了。开始我们的测试。 单线程调用测试:

public static void testOne() throws H

.length; j++) {

threads[j].join();

}

}finally{

httpclient.close();

}

}

/**

* A thread that performs a GET.

*/

static class GetThread extends Thread {

private final CloseableHttpClient httpClient;

private final HttpContext context;

private final HttpGet httpget;

private final int id;

publicGetThread(CloseableHttpClient httpClient, HttpGet httpget, int id) {

this.httpClient = httpClient;

this.context = new BasicHttpContext();

this.httpget = httpget;

this.id = id;

}

/**

* 执行 GetMethod,打印一些状态信息.

*/

@Override

public void run() {

try {

System.out.println(StringUtils.center(String.valueOf(id) + " "

+httpget.getURI(), 60, '-'));

CloseableHttpResponseresponse = httpClient.execute(httpget,

context);

try {

HttpEntityentity = response.getEntity();

if (entity != null) {

byte[] bytes = EntityUtils.toByteArray(entity);

System.out.println(StringUtils.center("线程" + id

+" 执行完毕" + bytes.length + " bytesread", 60,

"-"));

}

}finally{

response.close();

}

}catch(Exception e) {

System.out.println(id + " - error: "+ e);

}

}

} } 运行结果如下: 注意,每次运行的实际顺序可能和下面不一样,因为本示例没有严格控制现场的执行顺序。 ---1http://blog.csdn.net/ffm83/article/details/41944921---- -----------------2http://hotels.ctrip.com/----------------- ------------------3http://bus.ctrip.com/------------------- -----------------线程3 执行完毕108810 bytesread------------------ -----------------线程2 执行完毕171764 bytesread------------------ -------------------线程1 执行完毕162 bytesread------------------- runMutiThread used 365 ms.

多线程访问能够大幅度提升访问的效率,但是要确保线程之间的安全线,执行开始的顺序,和执行完毕的顺序并不完全相同。

原文

如果要httpclient模拟一个浏览器反复访问某一组页面,那么多线程是一个非常好的选择,超过一个以上的线程必须要用连接管理,主要工具类是ThreadSafeClientConnManager。 下面是示例代码:   pac

------分隔线----------------------------