Java:httpclient--the sixth chapter;

Source: Internet
Author: User


HttpClient--the first chapter;

HttpClient--chapter II;

HttpClient explanation--chapter three;

HttpClient--the fourth chapter;

HttpClient--the fifth chapter;

HttpClient--the sixth chapter;

HttpClient--the seventh chapter;


Compared to HttpURLConnection, httpclient richer, and more powerful, of which Apache has two items are httpclient, one is the commonts package, this is universal, More professional is the org.apache.http, so I generally use the latter;


HttpClient can handle long connections, save sessions, reconnect, and request filters, connection reuse, etc...


Here is the test code (all summarized from the official documentation, as well as the translation)


Need to download Core package: Httpclient-4.3.4.jar, can also be downloaded on the official website: http://hc.apache.org/downloads.cgi



/** * httpclient cache mechanism */private static void Test25 () throws Clientprotocolexception, IOException {cacheconfig cacheconf IG = Cacheconfig.custom (). Setmaxcacheentries (1000)//MAX Cache 1000 object. Setmaxobjectsize (8192)/        /byte. build (); Requestconfig requestconfig = Requestconfig.custom (). Setconnecttimeout (30000)//Request time-out. Setsocket    Timeout (30000)//Response time-out. Build (); A cached httpclient inherits all the configuration entries and parameters of the non-cached httpclient closeablehttpclient cachingclient = Cachinghttpclients.custom ().    Setcacheconfig (CacheConfig). Setdefaultrequestconfig (Requestconfig). build ();        Inherit httpclientcontext httpcachecontext context = Httpcachecontext.create ();    HttpGet httpget = new HttpGet ("http://www.mydomain.com/content/"); Cachingclient.execute (httpget, context); Cacheresponsestatus responsestatus = Context.getcacheresponsestatus ();/** * This cache request is still printed multiple times "the response from the upstream server ~ ~", not the desired result * here Have a few questions: Whether the caching mechanism requires the client and the server to work together * Also, if this cache configuration does not take effect, is the official website code wrong? */switch (responsestatus) {case CACHE_HIT:System.out.println ("the corresponding is generated by the cache and no request is sent to the upstream ~ ~"); Break;case Cache_module_ RESPONSE:SYSTEM.OUT.PRINTLN ("The response is generated directly from the cache ~ ~"); Break;case CACHE_MISS:System.out.println ("The response from the upstream server ~ ~"); break;case  VALIDATED:SYSTEM.OUT.PRINTLN ("The response is generated from the cache and verified to be from the source server ~"); }


Java:httpclient--the sixth chapter;

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.