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