HttpClient x-www-form-urlencoded

Source: Internet
Author: User

1. Submit a POST request using Apache HttpClient

HTTP tool method (need to specify encoding, otherwise error, UTF-8 used here)

 Public StaticString postwithparamsforstring (string URL, list<namevaluepair>params) {HttpClient Client=Httpclients.createdefault (); HttpPost HttpPost=Newhttppost (URL); String s= ""; Try{httppost.setentity (NewUrlencodedformentity (params, "UTF-8")); Httppost.setheader ("Content-type", "application/x-www-form-urlencoded"); HttpResponse Response=Client.execute (HttpPost); intStatusCode =response.getstatusline (). Getstatuscode (); if(statuscode==200) {httpentity entity=response.getentity (); S=entityutils.tostring (entity); }        } Catch(IOException e) {e.printstacktrace (); }        returns; }

Test method

 Public Static voidMain (string[] args) {String Smskey= "AAAAAAA"; String content= "Your order number is: 4322311"; String Phone= "111111"; String Smssecret= "BBBB"; String Smsurl= "Http://iccc/v1/message/content/send"; List<NameValuePair> params =NewArraylist<namevaluepair>(); String timestamp=string.valueof (System.currenttimemillis ()); String signcontent= "appkey=" + Smskey + "&content=" + content + "&mobile=" + Phone + "&timestamp=" + timestamp + "&appsecret=" +Smssecret; String Sign=Digestutils.md5hex (signcontent). toUpperCase (); Namevaluepair pair=NewBasicnamevaluepair ("Appkey", Smskey); Namevaluepair Pair2=NewBasicnamevaluepair ("Content", content); Namevaluepair Pair3=NewBasicnamevaluepair ("mobile", phone); Namevaluepair PAIR4=NewBasicnamevaluepair ("timestamp", timestamp); Namevaluepair PAIR5=NewBasicnamevaluepair ("Sign", sign);        Params.add (pair);        Params.add (PAIR2);        Params.add (PAIR3);        Params.add (PAIR4);        Params.add (PAIR5); String postforstring= Httputil.postwithparamsforstring (Smsurl, params);
}

2. Okhttp implementation

Importokhttp3.*;Importorg.apache.commons.codec.digest.DigestUtils;Importjava.io.IOException;ImportJava.util.concurrent.TimeUnit;/*** Created by admin on 2018/1/22.*/ Public classSmssender {Private  Static FinalOkhttpclient client =NewOkhttpclient.builder (). ConnectionPool (NewConnectionPool (100,10, Timeunit.minutes)) . ConnectTimeout (5, Timeunit.seconds). ReadTimeout (5, Timeunit.seconds). Build ();  Public Staticstring postformbody (string url, formbody body) {Request Request=Newrequest.builder (). URL (URL). Post (body). Build (); Try{Response Response=Client.newcall (Request). Execute (); returnresponse.body (). String (); } Catch(IOException e) {e.printstacktrace (); }        return""; }     Public Static voidMain (string[] args)throwsIOException {String Content= "Your order number is: 4322311"; String mobile = "1314455"; String timestamp=string.valueof (System.currenttimemillis ()); String signcontent= "CC"; String Sign=Digestutils.md5hex (signcontent). toUpperCase (); Formbody Body=NewFormbody.builder (). Add ("Content", content). Add ("mobile", Mobile). Add ("Timestamp", timestamp). Add ("sign"), sign). build (); String URL= "Dd/v1/message/content/send"; String result=postformbody (URL, body);    SYSTEM.OUT.PRINTLN (result); }}

3. Post Test

Select x-www-form-urlencoded to enter the corresponding key value

HttpClient x-www-form-urlencoded

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.