版权声明:本文为博主原创文章,未经博主允许不得转载。如需转载请声明:【转自 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"); } ~~~      测试结果如下: ![](https://box.kancloud.cn/2016-02-18_56c53cb8202c9.jpg)      可以看到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(); } ~~~      测试结果如下: ![](https://box.kancloud.cn/2016-02-18_56c53cb83c6b2.jpg)      从结果中可以清楚的看到执行1000次调用,总消耗是51165,平均51ms/个,速度快,而且没有报错。      好了,本工具就分享到这里,下次会分享异步的HttpClient,敬请期待。      代码已上传至:[https://github.com/Arronlong/httpclientUtil](https://github.com/Arronlong/httpclientUtil)。