? ? ?版權聲明:本文為博主原創文章,未經博主允許不得轉載。如需轉載請聲明:【轉自 http://blog.csdn.net/xiaoxian8023 】
? ? ?本文主要來分享一下該工具類的測試結果。工具類的整體源碼不再單獨分享,源碼基本上都已經在文章中了。開始我們的測試。
? ? ?單線程調用測試:
~~~
public static void testOne() throws HttpProcessException{
System.out.println("--------簡單方式調用(默認post)--------");
String url = "http://tool.oschina.net/";
//簡單調用
String resp = HttpClientUtil.send(url);
System.out.println("請求結果內容長度:"+ resp.length());
System.out.println("\n#################################\n");
System.out.println("--------加入header設置--------");
url="http://blog.csdn.net/xiaoxian8023";
//設置header信息
Header[] headers=HttpHeader.custom().userAgent("Mozilla/5.0").build();
//執行請求
resp = HttpClientUtil.send(url, headers);
System.out.println("請求結果內容長度:"+ resp.length());
System.out.println("\n#################################\n");
System.out.println("--------代理設置(繞過證書驗證)-------");
url="https://www.facebook.com/";
HttpClient client= HCB.custom().timeout(10000).proxy("127.0.0.1", 8087).ssl().build();//采用默認方式(繞過證書驗證)
//執行請求
resp = HttpClientUtil.send(client,url);
System.out.println("請求結果內容長度:"+ resp.length());
System.out.println("\n#################################\n");
System.out.println("--------代理設置(自簽名證書驗證)+header+get方式-------");
url = "https://sso.tgb.com:8443/cas/login";
client= HCB.custom().timeout(10000).ssl("D:\\keys\\wsriakey","tomcat").build();
headers=HttpHeader.custom().keepAlive("false").connection("close").contentType(Headers.APP_FORM_URLENCODED).build();
//執行請求
resp = HttpClientUtil.send(client, url, HttpMethods.GET, headers);
System.out.println("請求結果內容長度:"+ resp.length());
System.out.println("\n#################################\n");
}
~~~
? ? ?測試結果如下:

? ? ?可以看到4次調用,都沒有問題。
? ? ?那么現在試試多線程調用吧。我定義一個數組,里面有20篇文章的地址。我啟動20個線程的線程池來測試,寫了一個20*50次的for循環,看看全部線程結束時有沒有報錯,能用多長時間:
~~~
public static void testMutilTask(){
// URL列表數組
String[] urls = {
"http://blog.csdn.net/xiaoxian8023/article/details/49862725",
"http://blog.csdn.net/xiaoxian8023/article/details/49834643",
"http://blog.csdn.net/xiaoxian8023/article/details/49834615",
"http://blog.csdn.net/xiaoxian8023/article/details/49834589",
"http://blog.csdn.net/xiaoxian8023/article/details/49785417",
"http://blog.csdn.net/xiaoxian8023/article/details/48679609",
"http://blog.csdn.net/xiaoxian8023/article/details/48681987",
"http://blog.csdn.net/xiaoxian8023/article/details/48710653",
"http://blog.csdn.net/xiaoxian8023/article/details/48729479",
"http://blog.csdn.net/xiaoxian8023/article/details/48733249",
"http://blog.csdn.net/xiaoxian8023/article/details/48806871",
"http://blog.csdn.net/xiaoxian8023/article/details/48826857",
"http://blog.csdn.net/xiaoxian8023/article/details/49663643",
"http://blog.csdn.net/xiaoxian8023/article/details/49619777",
"http://blog.csdn.net/xiaoxian8023/article/details/47335659",
"http://blog.csdn.net/xiaoxian8023/article/details/47301245",
"http://blog.csdn.net/xiaoxian8023/article/details/47057573",
"http://blog.csdn.net/xiaoxian8023/article/details/45601347",
"http://blog.csdn.net/xiaoxian8023/article/details/45569441",
"http://blog.csdn.net/xiaoxian8023/article/details/43312929",
};
// 設置header信息
Header[] headers = HttpHeader.custom().userAgent("Mozilla/5.0").build();
HttpClient client= HCB.custom().timeout(10000).build();
long start = System.currentTimeMillis();
try {
int pagecount = urls.length;
ExecutorService executors = Executors.newFixedThreadPool(pagecount);
CountDownLatch countDownLatch = new CountDownLatch(pagecount*100);
for(int i = 0; i< pagecount*100;i++){
//啟動線程抓取
executors.execute(new GetRunnable(urls[i%pagecount], headers, countDownLatch).setClient(client));
}
countDownLatch.await();
executors.shutdown();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("線程" + Thread.currentThread().getName() + ", 所有線程已完成,開始進入下一步!");
}
long end = System.currentTimeMillis();
System.out.println("總耗時(毫秒): -> " + (end - start));
//(7715+7705+7616)/3= 23 036/3= 7 678.66---150=51.2
//(9564+8250+8038+7604+8401)/5=41 857/5=8 371.4--150
//(9803+8244+8188+8378+8188)/5=42 801/5= 8 560.2---150
}
static class GetRunnable implements Runnable {
private CountDownLatch countDownLatch;
private String url;
private Header[] headers;
private HttpClient client = null;
public GetRunnable setClient(HttpClient client){
this.client = client;
return this;
}
public GetRunnable(String url, Header[] headers,CountDownLatch countDownLatch){
this.url = url;
this.headers = headers;
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
try {
String response = null;
if(client!=null){
response = HttpClientUtil.send(client, url, headers);
}else{
response = HttpClientUtil.send(url, headers);
}
System.out.println(Thread.currentThread().getName()+"--獲取內容長度:"+response.length());
} catch (HttpProcessException e) {
e.printStackTrace();
} finally {
countDownLatch.countDown();
}
}
}
~~~
? ? ?定義了一個ExecutorService的線程池,使用CountDownLatch來保證所有線程都運行完畢,測試一下看看:
~~~
public static void main(String[] args) throws Exception {
// testOne();
testMutilTask();
}
~~~
? ? ?測試結果如下:

? ? ?從結果中可以清楚的看到執行1000次調用,總消耗是51165,平均51ms/個,速度快,而且沒有報錯。
? ? ?好了,本工具就分享到這里,下次會分享異步的HttpClient,敬請期待。
? ? ?代碼已上傳至:[https://github.com/Arronlong/httpclientUtil](https://github.com/Arronlong/httpclientUtil)。
- 前言
- HttpClient3.x之Get請求和Post請求示例
- httpclient3.x中使用HTTPS的方法
- 簡單的利用UrlConnection,后臺模擬http請求
- 輕松把玩HttpClient之模擬post請求示例
- 輕松把玩HttpClient之配置ssl,采用繞過證書驗證實現https
- 輕松把玩HttpClient之配置ssl,采用設置信任自簽名證書實現https
- 輕松把玩HttpClient之設置代理,可以訪問FaceBook
- 輕松把玩HttpClient之封裝HttpClient工具類(一)(現有網上分享中的最強大的工具類)
- 輕松把玩HttpClient之封裝HttpClient工具類(二),插件式配置HttpClient對象
- 輕松把玩HttpClient之封裝HttpClient工具類(三),插件式配置Header
- 輕松把玩HttpClient之封裝HttpClient工具類(四),單線程調用及多線程批量調用測試
- 輕松把玩HttpAsyncClient之模擬post請求示例
- 輕松把玩HttpClient之封裝HttpClient工具類(五),攜帶Cookie的請求
- 輕松把玩HttpClient之封裝HttpClient工具類(六),封裝輸入參數,簡化工具類