<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ? ? ?版權聲明:本文為博主原創文章,未經博主允許不得轉載。如需轉載請聲明:【轉自 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)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看