Java sends HTTP requests--httpclient

Source: Internet
Author: User

Use HttpClient to send HTTP requests

Introduction of two packages: [1]org.apache.httpcomponents. httpclient_x.x.x.jar [2]org.apache.httpcomponents. Httpcore_x.x.x.jar

Download Link:Apache httpcomponents-httpcomponents Downloads

Reference document: [1]httpclient Tutorial [2]httpclient Example

1  Packagehttp;2 3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 6 ImportOrg.apache.http.Header;7 Importorg.apache.http.HttpEntity;8 Importorg.apache.http.ProtocolVersion;9 ImportOrg.apache.http.StatusLine;Ten Importorg.apache.http.client.ClientProtocolException; One ImportOrg.apache.http.client.config.RequestConfig; A ImportOrg.apache.http.client.methods.CloseableHttpResponse; - ImportOrg.apache.http.client.methods.HttpGet; - Importorg.apache.http.impl.client.CloseableHttpClient; the Importorg.apache.http.impl.client.HttpClients; -  -  -  +  Public classHttpConnector { -      Public Static voidMain (string[] args) { +Closeablehttpclient httpClient =Httpclients.createdefault (); ARequestconfig Requestconfig =Requestconfig.custom () at. setconnecttimeout (1000) -. setsockettimeout (1000) - . Build (); -HttpGet get =NewHttpGet ("http://i.easou.com/s.m?q= movie &wver=t"); - Get.setconfig (requestconfig); -          in         /*         - HttpPost post = new HttpPost ("HTTP://I.EASOU.COM/S.M"); to Post.setconfig (requestconfig); + list<namevaluepair> namevaluepairs = new arraylist<namevaluepair> (); - Namevaluepairs.add (New Basicnamevaluepair ("Q", "film")); the Namevaluepairs.add (New Basicnamevaluepair ("Wver", "T")); * urlencodedformentity urlentity; $ urlentity = new urlencodedformentity (namevaluepairs,consts.utf_8);Panax Notoginseng post.setentity (urlentity); -         */ theCloseablehttpresponse response =NULL; +         Try { AResponse =Httpclient.execute (get); the             /* + response = Httpclient.execute (post); -             */ $Statusline Statusline =response.getstatusline (); $ProtocolVersion protocolversion =statusline.getprotocolversion (); -String reasonphrase =statusline.getreasonphrase (); -             intStatusCode =Statusline.getstatuscode (); theSystem.out.println (statusline + "\ n" + protocolversion + "+ StatusCode +" "+reasonphrase); -             /*#############################################################################################*/WuyiHttpentity httpentity =response.getentity (); the             if(Httpentity! =NULL) { -Header ContentType =Httpentity.getcontenttype (); WuHeader contentencoding =httpentity.getcontentencoding (); -                 LongContentLength =httpentity.getcontentlength (); About                 BooleanIsstreaming =httpentity.isstreaming (); $                 BooleanIsrepeatable =httpentity.isrepeatable (); -                 Booleanischunked =httpentity.ischunked (); -System.out.println (ContentType + "|" + contentencoding + "|" + ContentLength + "|" + isstreaming + "/" + ISREPEATABL E + "/" +ischunked); -                 /*#############################################################################################*/ AInputStream is =httpentity.getcontent (); +                 byte[] Buff =New byte[512]; the                  while(Is.read (Buff) > 0) { -                      for(byteB:buff) { $System.out.print ((Char) b); the                     } the                 } the is.close (); the                 /*#############################################################################################*/ -             } in}Catch(clientprotocolexception e) { the e.printstacktrace (); the}Catch(IOException e) { About e.printstacktrace (); the}finally { the             if(Response! =NULL) { the                 Try { + response.close (); -}Catch(IOException e) { the e.printstacktrace ();Bayi                 } the             } the         } -         Try { - httpclient.close (); the}Catch(IOException e) { the e.printstacktrace (); the         } the     } -      the}

Java sends HTTP requests--httpclient

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.