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 + "×tamp=" + 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