Java:httpclient specific explanation-fifth chapter;

Source: Internet
Author: User
Tags html form



HttpClient specific explanation-the first chapter;

HttpClient specific explanation-chapter II;

HttpClient specific explanation-Chapter III;

HttpClient specific explanation-fourth chapter;

HttpClient specific explanation-fifth chapter;

HttpClient specific explanation-sixth chapter;

httpclient Specific explanation-seventh chapter;



Compared to HttpURLConnection, httpclient is richer and more powerful, in which Apache has two projects 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...


The following is the test code (all summaries are from official documents, as well as translations)


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






--------------------------High-Speed API---------------------------------/** * High-speed API provides only the most important features for scenarios that do not require flexible scaling */private static void Test22 () throws clientprotocolexception, ioexception{string result = request.get ("/HTTP/ Webservice.webxml.com.cn/webservices/mobilecodews.asmx/getdatabaseinfo "). ConnectTimeout (1000)// Sets the server request time-out. Sockettimeout (1000)//set server corresponding timeout. Execute (). Returncontent (). asstring (); String result2 = Request.post ("Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getDatabaseInfo"). Useexpectcontinue (). Version (Httpversion.http_1_1). Bodystring ("Number of references", Contenttype.default_text). Execute (). Returncontent (). asstring ();//submit an HTML form and save the returned result Request.post ("http://webservice.webxml.com.cn/WebServices/ Mobilecodews.asmx/getdatabaseinfo "). AddHeader (" X-custom-header "," stuff ")//The form header. Viaproxy (New Httphost ("  Myproxy ", 8080))//Set agent. BodyForm (Form.form ()//forms. Add (" Mobilecode "," 12345 "). Add (" UserID ", "123456"). Build ()). execUte (). Savecontent (New File ("Result.txt")); SYSTEM.OUT.PRINTLN (result); System.out.println (RESULT2);} /** * Using executor high-speed development; * Assuming you need to run certain requests in a specified security context, we can also use exector directly, * This is when the user's authentication information is cached for possible requests.              */private static void test23 () throws Clientprotocolexception, ioexception{Executor Executor = executor.newinstance () . Auth (New Httphost ("Somehost", 8080), "username", "password")//Join the certification. Authpreemptive (New Httphost ("s  Omehost ", 8080));        Use the Preemptive authentication Executor.execute (Request.get ("http://somehost/"))//Run the Get request. Returncontent (). asstring (); Executor.execute (Request.post ("http://somehost/")//Run the Post request. Useexpectcontinue (). bodystring ("Imp    Ortant stuff ", Contenttype.default_text)). Returncontent (). asstring (); }/** * * High-speed response processing * * Use request to send a GET request at high speed and return the result with the ResponseHandler callback; */private static void Test24 () throws Clientprotocolexc Eption, IOException {Object result = Request. Get ("Http://webservice.webxml.com.cN/webservices/mobilecodews.asmx/getdatabaseinfo "). Execute (). Handleresponse (New responsehandler<object> () { Public Object handleresponse (final HttpResponse response) throws IOException {Statusline Statusline = Response.getstatusline (); if (Statusline.getstatuscode () ==200) {httpentity entity = response.getentity (); if (Entity! = NULL) {String str = entityutils.tostring (entity); return str;}} return null;}}); if (result! = null) {System.out.println (">>>>>>" +result);}}



Java:httpclient specific explanation-fifth 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.