The httpclient of httpcomponents

Source: Internet
Author: User

httpcomponents Source directory: http://www.boyunjian.com/javasrc/org.apache.httpcomponents/httpclient/4.3.4/_/

Packagecom.bbkj.utils;ImportCom.alibaba.fastjson.JSON;Importorg.apache.http.HttpEntity;ImportOrg.apache.http.HttpHost;ImportOrg.apache.http.HttpResponse;ImportOrg.apache.http.NameValuePair;Importorg.apache.http.client.HttpClient;ImportOrg.apache.http.client.config.RequestConfig;Importorg.apache.http.client.methods.HttpUriRequest;ImportOrg.apache.http.client.methods.RequestBuilder;ImportOrg.apache.http.conn.routing.HttpRoute;ImportOrg.apache.http.entity.ContentType;Importorg.apache.http.entity.StringEntity;Importorg.apache.http.impl.client.CloseableHttpClient;ImportOrg.apache.http.impl.client.HttpClientBuilder;Importorg.apache.http.impl.client.HttpClients;ImportOrg.apache.http.impl.conn.PoolingHttpClientConnectionManager;ImportOrg.apache.http.message.BasicNameValuePair;Importorg.apache.http.util.EntityUtils;ImportJava.util.*; Public classhttputils{Private StaticPoolinghttpclientconnectionmanager ConnectionManager =NULL; Private StaticHttpclientbuilder Httpbulder =NULL; Private StaticRequestconfig Requestconfig =NULL; Private Static intMaxconnection = 10; Private Static intDefaultmaxconnection = 5; Private StaticString IP = "ABC.com"; Private Static intPORT = 80; Static { //Setting the status parameters for HTTPRequestconfig =Requestconfig.custom (). SetSocketTimeout (5000). Setconnecttimeout (5000). Setconnectionrequesttimeout (5000). build (); Httphost Target=Newhttphost (IP, PORT); ConnectionManager=NewPoolinghttpclientconnectionmanager (); Connectionmanager.setmaxtotal (maxconnection); Connectionmanager.setdefaultmaxperroute (defaultmaxconnection); Connectionmanager.setmaxperroute (NewHttproute (target), 20); Httpbulder=Httpclients.custom (); Httpbulder.setconnectionmanager (ConnectionManager); } Public Staticcloseablehttpclient getconnection () {closeablehttpclient httpClient=Httpbulder.build (); HttpClient=Httpbulder.build (); returnhttpClient; } Public StaticHttpurirequest getrequestpost (map<string, object>map, String URL) {List<NameValuePair> params =NewArraylist<namevaluepair>(); Set<map.entry<string, object>> entryset =Map.entryset (); Httpurirequest Reqmethod=NULL; Stringentity stringentity=Newstringentity (json.tojsonstring (map), Contenttype.application_json); Reqmethod=requestbuilder.post (). Seturi (URL). setentity (stringentity). Setconfig (Requestco Nfig). build (); returnReqmethod; } Public StaticHttpurirequest Getrequestget (map<string, string>map, String URL) {List<NameValuePair> params =NewArraylist<namevaluepair>(); Set<map.entry<string, string>> entryset =Map.entryset (); for(Map.entry<string, string>E:entryset) {String name=E.getkey (); String value=E.getvalue (); Namevaluepair pair=NewBasicnamevaluepair (name, value); Params.add (pair); } httpurirequest Reqmethod=NULL; Reqmethod=requestbuilder.get (). Seturi (URL). AddParameters (Params.toarray (Newbasicnamevaluepair[params.size ()])) . Setconfig (Requestconfig). build (); returnReqmethod; } Public Static voidMain (String args[])throwsException {Map<string, object> map =NewHashmap<string, object>();//map.put ("startindex", "1");//map.put ("Size", "2");Map.put ("SN", "1"); Map.put ("Index", 1); Map.put ("Heart_rate", 1); Map.put ("Resp_rate", 1); Map.put ("Leave_bed",true); Map.put ("Leave_bed_time", 1); Map.put ("Timestamp", 1); Map.put ("Bedno", "1"); /*** * String SN; Device number Integer index; Ordinal Integer heart_rate; Heart rate Integer resp_rate; Breath/minute Boolean leave_bed; False: Not away from bed, true: Leave bed Integer leave_bed_time; Away from bed time (minutes) long timestamp; String Bedno; */HttpClient Client=getconnection ();Httpurirequest post = getrequestpost (map, "Http://abc.com/bed/submit/data"); HttpResponse Response=Client.execute (POST); System.out.println (Response.getstatusline (). Getstatuscode ()); if(Response.getstatusline (). Getstatuscode () = = 200) {httpentity entity=response.getentity (); String message= Entityutils.tostring (Entity, "Utf-8"); SYSTEM.OUT.PRINTLN (message); } Else{System.out.println ("Request Failed"); } }}

The httpclient of httpcomponents

Related Article

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.