Method One: HttpClient
Public voidposttest (httpservletrequest request,integer type,string phone,string passwd,string schoolld,String agent) {STR ing URL= "Xxxxxxxxx";//URL address of the sending requestJsonobject Jsonobject =Newjsonobject ();//package parameter Jsonobject.put ("Phone", "XXXX");Jsonobject.put ("Agent", "XXXX"); String params=jsonobject.tostring (); Sendposthttpclient (URL, params);} Publicstring sendposthttpclient (string URL, string data) {string Response=NULL; Try{closeablehttpclient httpclient=NULL; Closeablehttpresponse HttpResponse=NULL; Try{httpclient=Httpclients.createdefault (); HttpPost HttpPost=Newhttppost (URL); //stringentity stringentity = new Stringentity (data, Contenttype.create ("Text/json", "UTF-8")), parameter passing text formatStringentity stringentity =NewStringentity (data, Contenttype.create ("Application/json", "UTF-8"));//parameter pass JSON formathttppost.setentity (stringentity); HttpResponse=Httpclient.execute (HttpPost); Response=entityutils.tostring (Httpresponse.getentity ()); Logger.info ("Response:" +response); } finally { if(HttpClient! =NULL) {httpclient.close (); } if(HttpResponse! =NULL) {httpresponse.close (); } } } Catch(Exception e) {e.printstacktrace (); } returnresponse;}
Method Two: Resttemplate
PublicMap<string,string>Getshanghaicardealcount (String formattime) {Map<String,String> map=NewHashmap<>(); Map.put ("Flag", "fail"); String Cycle= "1"; String Pic_type= "3"; String QSRQ=Formattime; String Submit= "Display"; String Zzrq=Formattime; String result=NULL; Try{String URL= "Http://xxxxxxxxxxxxxxxxxxxxxx"; Resttemplate Client=Newresttemplate (); Httpheaders Headers=Newhttpheaders (); //do not easily change this submission method, most of the cases, the submission method is the form submissionHeaders.setcontenttype (mediatype.application_form_urlencoded); //package parameters, do not replace with map and hashmap, otherwise parameters cannot be passedMultivaluemap<string, string> params=NewLinkedmultivaluemap<string, string>(); //also supports ChineseParams.add ("Cycle", cycle); Params.add ("Pic_type", Pic_type); Params.add ("Qsrq", QSRQ); Params.add ("Submit", Submit); Params.add ("Zzrq", ZZRQ); Httpentity<multivaluemap<string, string>> requestentity =NewHttpentity<multivaluemap<string, string>>(params, headers); //executing an HTTP requestresponseentity<string> response = Client.exchange (URL, httpmethod.post, requestentity, String.class); Result=Response.getbody (); Map.put ("Result", result); Map.put ("Flag", "success"); } Catch(Exception e) {e.printstacktrace (); } returnmap; }
Java analog send POST request test