Java analog send POST request test

Source: Internet
Author: User

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

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.