HttpClient4.5 sending an HTTP request depth example using an HTTP connection pool

Source: Internet
Author: User

HttpClient 3.x,4.x provides an HTTP connection pool manager, and when a request connection pool manager (such as Poolinghttpclientconnectionmanager) is used, HttpClient can simultaneously execute requests from multiple threads.

Earlier versions of hc3.x and 4.x provide classes such as poolingclientconnectionmanager,defaulthttpclient to implement HTTP connection pooling, but these classes are largely obsolete after the 4.3.x version. This article uses the latest poolinghttpclientconnectionmanager, such as the 4.3.x, to implement HTTP connection pooling.
Needless to say, the following is all the code:

Public class pooltest {    private static void config ( Httprequestbase httprequestbase)  {         Httprequestbase.setheader ("User-agent",  "mozilla/5.0");         Httprequestbase.setheader ("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");         httprequestbase.setheader ("Accept-language",  "zh-CN,zh;q=0.8, en-us;q=0.5,en;q=0.3 "),//" en-us,en;q=0.5 ");         Httprequestbase.setheader ("Accept-charset",  "iso-8859-1,utf-8,gbk,gb2312;q=0.7,*;q=0.7");                 //  Configure timeout settings for requests          requestconfig requestconfig = requestconfig.custom ()            &Nbsp;    .setconnectionrequesttimeout (       )          .setconnecttimeout (     )            .setsockettimeout (   )              .build ();         httprequestbase.setconfig (Requestconfig);             }    public static void main (String[]  args)  {        connectionsocketfactory plainsf =  plainconnectionsocketfactory.getsocketfactory ();         Layeredconnectionsocketfactory sslsf = sslconnectionsocketfactory.getsocketfactory ();         rEgistry<connectionsocketfactory> registry = registrybuilder.<connectionsocketfactory >create ()                 . Register ("http", &NBSP;PLAINSF)                  .register ("https", &NBSP;SSLSF)                  .build ();         Poolinghttpclientconnectionmanager cm = new poolinghttpclientconnectionmanager (registry);         //  increase the maximum number of connections to 200         cm.setmaxtotal (;        // ) increases the connection for each routing base to 20         cm.setdefaultmaxperroute (;       )  //  increase the maximum number of connections for the target host to 50&NBSP;&NBSp;      httphost localhost = new httphost ("HTTP/ Blog.csdn.net/gaolu,         cm.setmaxperroute (New HttpRoute ( localhost),  50);                 //Request Retry Processing         HttpRequestRetryHandler  Httprequestretryhandler = new httprequestretryhandler ()  {             public boolean retryrequest (IOException exception,int  executioncount, httpcontext context)  {                 if  (executioncount >= 5)  {//  If you have retried 5 times, give up                                          return  false;                }                 if  (exception  instanceof nohttpresponseexception)  {//  If the server loses its connection, retry                                           return  true;                }                 if  ( exception instanceof sslhandshakeexception)  {//  do not retry the SSL handshake exception                                           return false;                 }                                  if  (exception instanceof  interruptedioexception)  {//  Timeout                                           return false;                 }                 if  (exception instanceof unknownhostexception)  {//  target server not up to                                            return false;                 }                 if  (exception instanceof connecttimeoutexception)  {//  connection denied                                            return false;                 }                if  (exception  instanceof sslexception)  {// ssl Handshake Exception                                           return false;                 }                                  httpclientcontext clientcontext = &NBSP;HTTPCLIENTCONTEXT.ADAPT (context);                 httprequest request = clientcontext.getrequest ();                 //  if the request is idempotent, try again                  if  (!) ( request instanceof httpentityenclosingrequest))  {                                          return true;                 }                 return false;             }         };                   closeablehttpclient httpclient = httpclients.cUstom ()                 . Setconnectionmanager (cm)                  .setretryhandler (Httprequestretryhandler)                  .build ();        //  URL List Array         String[] urisToGet = {                            "http://blog.csdn.net/gaolu/article/details/48466059",                  "http://blog.csdn.net/gaolu/article/details/ 48243103 ",                " Http://blog.csdn.net/gaolu/article/details/47656987 ",                  "http://blog.csdn.net/gaolu/article/details/47055029",                                   "http://blog.csdn.net/gaolu/article/details/46400883",                  "http://blog.csdn.net/ gaolu/article/details/46359127 ",                  "http://blog.csdn.net/gaolu/article/details/46224821",                  "http://blog.csdn.net/gaolu/article/details/ 45305769 ",                                  "http://blog.csdn.net/gaolu/article/details/ 43701763 ",                " http://blog.csdn.net/gaolu/article/details/43195449 ",                  "http://blog.csdn.net/gaolu/article/details/42915521",                  "http://blog.csdn.net/ gaolu/article/details/41802319 ",                                   "http://blog.csdn.net/gaolu/article/details/41045233",                  "http://blog.csdn.net/gaolu/article/details/40395425",                  "http://blog.csdn.net/gaolu/article/details/40047065",                  "HTTP +/ blog.csdn.net/gaolu/article/details/39891877 ",                                   "http://blog.csdn.net/gaolu/article/details/39499073",                  "http://blog.csdn.net/gaolu/article/details/ 39314327 ",                " http://blog.csdn.net/gaolu/article/details/38820809 ",                  "http://blog.csdn.net/gaolu/article/details/38439375",        &nBSP;};                 long  Start = system.currenttimemillis ();                 try {             int pagecount = urisToGet.length;             executorservice executors = executors.newfixedthreadpool (PageCount);             countdownlatch countdownlatch  = new countdownlatch (PageCount);                      for (int i = 0; i<  pagecount;i++) {                 httpget httpget  = new httpget (Uristoget[i]);                 config (HttpGet);                 //Start Thread Crawl                  executors.execute (New getrunnable (Httpclient,httpget,countdownlatch));             }             countdownlatch.await ();             executors.shutdown ();         } catch   (interruptedexception e)  {             e.printstacktrace ();        } finally {         &nBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Thread"  + thread.currentthread (). GetName ()  +  ","  + system.currenttimemillis ()  +  ",  All threads are complete, start to go to the next step!" ");        }                         long end =  system.currenttimemillis ();         system.out.println ("Consume  ->  " +  (end - start));    }         static class GetRunnable implements Runnable {         private CountDownLatch countDownLatch;         private final CloseableHttpClient httpClient;         private final httpget httpgEt;                public  getrunnable (closeablehttpclient httpclient, httpget httpget, countdownlatch  Countdownlatch) {            this.httpclient =  httpclient;            this.httpget =  httpget;                         this.countDownLatch = countDownLatch;         }         @Override          public void run ()  {             CloseableHttpResponse response = null;             try {                 response = httpclient.execute (Httpget,httpclientcontext.create ());                 httpentity entity  = response.getentity ();                 system.out.println (entityutils.tostring (entity,  "Utf-8"))  ;                 entityutils.consume (Entity );            } catch  (IOException  e)  {                 E.printstacktrace ();             } finally  {     &Nbsp;          countdownlatch.countdown ();                                  try {                     if (response !=  null)                          response.close ();                 } catch  (ioexception e)  {                      e.printstacktrace ();                 }             }        }     }    }

Main reference documents:
http://free0007.iteye.com/blog/2012308

HttpClient4.5 sending an HTTP request depth example using an HTTP connection pool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.