These days, the boss let me get a message verification code to send the function, the use of the letter easy access to the SMS platform interface, and then in Java using httpclient analog POST request or GET request (see the message platform requirements, the general situation is a POST request), Invoke the interface provided by the SMS platform (Follow the interface specification of the SMS platform). See the code specifically:
When using httpclient, you need to introduce it in your project:
Commons-httpclient-3.1.jar
This jar package,
Project structure:
1. Create an HTTP impersonation Request tool class, then write a post method or Get method
/** * File description * @Description: extension Note * @Copyright: Dreamtech.com.cn Inc. All right reserved * @Version: V6.0 */ PackageCom.demo.util;ImportJava.io.IOException;ImportJava.util.Map;ImportOrg.apache.commons.httpclient.HttpClient;ImportOrg.apache.commons.httpclient.HttpException;ImportOrg.apache.commons.httpclient.SimpleHttpConnectionManager;ImportOrg.apache.commons.httpclient.methods.GetMethod;ImportOrg.apache.commons.httpclient.methods.PostMethod;/** * @Author: Feizi * @Date: April 17, 2015 Morning 9:26:34 * @ModifyUser: Feizi * @ModifyDat E: April 17, 2015 Morning 9:26:34 * @Version: V6.0 */ Public class httprequestutil { /** * HttpClient Analog POST Request * Method description * @Discription: Extension description * @param URL * @par Am params * @return String * @Author: Feizi * @Date: April 17, 2015 7:15:59 * @ModifyUser: Feizi * @ModifyDate: April 17, 2015 afternoon 7:15:59 * * Public StaticStringpostrequest(String URL, map<string, string> params) {//Example of constructing httpclientHttpClient HttpClient =NewHttpClient ();//Create an instance of the Post methodPostmethod Postmethod =NewPostmethod (URL);//Set request header informationPostmethod.setrequestheader ("Connection","Close");//Add parameter for(Map.entry<string, String> entry:params.entrySet ()) {Postmethod.addparameter (Entry.getkey (), Entry.getvalue ()); }//Use the default recovery policy provided by the system to set the request retry processing with the default retry processing: request three timesHttpclient.getparams (). Setbooleanparameter ("Http.protocol.expect-continue",false);//Receive processing resultsString result =NULL;Try{//Execute HTTP POST requestHttpclient.executemethod (Postmethod);//Return processing resultsresult = Postmethod.getresponsebodyasstring (); }Catch(HttpException e) {//Fatal exception, either protocol is incorrect or the content returned is problematicSystem.out.println ("Please check the input url!"); E.printstacktrace (); }Catch(IOException e) {//Network exception occurredSystem.out.println ("A network exception has occurred!"); E.printstacktrace (); }finally{//Release linkPostmethod.releaseconnection ();//Close HttpClient instance if(HttpClient! =NULL) {((Simplehttpconnectionmanager) Httpclient.gethttpconnectionmanager ()). Shutdown (); HttpClient =NULL; } }returnResult }/** * HttpClient Analog GET Request * Method description * @Discription: Extension description * @param URL * @par Am params * @return String * @Author: Feizi * @Date: April 17, 2015 7:15:28 * @ModifyUser: Feizi * @ModifyDate: April 17, 2015 afternoon 7:15:28 * * Public StaticStringgetrequest(String URL, map<string, string> params) {//Construct HttpClient instanceHttpClient client =NewHttpClient ();//Stitching parametersString Paramstr =""; for(String Key:params.keySet ()) {paramstr = Paramstr +"&"+ key +"="+ Params.get (key); } PARAMSTR = Paramstr.substring (1);//Create an instance of the Get methodGetMethod method =NewGetMethod (URL +"?"+ paramstr);//Receive return resultsString result =NULL;Try{//Execute HTTP GET method requestClient.executemethod (method);//Return processing resultsresult = Method.getresponsebodyasstring (); }Catch(HttpException e) {//Fatal exception, either protocol is incorrect or the content returned is problematicSystem.out.println ("Please check the input url!"); E.printstacktrace (); }Catch(IOException e) {//Network exception occurredSystem.out.println ("A network exception has occurred!"); E.printstacktrace (); }finally{//Release linkMethod.releaseconnection ();//Close HttpClient instance if(Client! =NULL) {((Simplehttpconnectionmanager) Client.gethttpconnectionmanager ()). Shutdown (); Client =NULL; } }returnResult }}
2, in the creation of a class, generate verification code, and then pass the corresponding parameters (different SMS platform interface will have different parameter requirements, the general text message platform provided by the interface document will be available, directly read the document and then as required)
/** * File description * @Description: extension Note * @Copyright: Dreamtech.com.cn Inc. All right reserved * @Version: V6.0 */ PackageCom.demo.util;ImportJava.net.URLEncoder;ImportJava.util.HashMap;ImportJava.util.Map;/** * @Author: Feizi * @Date: April 17, 2015 Morning 9:24:48 * @ModifyUser: Feizi * @ModifyDat E: April 17, 2015 Morning 9:24:48 * @Version: V6.0 */ Public class sendmsgutil { /** * Send SMS Message * Method description * @Discription: Extension description * @param phones * @param Co Ntent * @return * @return String * @Author: Feizi * @Date: April 2015 17th pm 7:18:08 * @ModifyUser: Feizi * @ModifyDate: April 17, 2015 PM 7:18:08 */ @SuppressWarnings("Deprecation") Public StaticStringsendmsg(String phones,string content) {//SMS interface URL submission addressString URL ="SMS interface URL submission Address"; map<string, string> params =NewHashmap<string, string> (); Params.put ("en","User account"); Params.put ("MM","User Password"); Params.put ("Dxlbid","SMS category number"); Params.put ("Extno","extension number");//Mobile phone number, multiple numbers using commas for segmentationParams.put ("HM", phones);//Urlencoder encoding of SMS contentParams.put ("NR", Urlencoder.encode (content));returnHttprequestutil.getrequest (URL, params); }/** * Randomly generated 6-bit random Verification Code * Method Description * @Discription: Extension description * @return * @return St Ring * @Author: Feizi * @Date: April 17, 2015 PM 7:19:02 * @ModifyUser: Feizi *
@ModifyDate: April 17, 2015 PM 7:19:02 * *
Public StaticStringCreaterandomvcode(){//Verification CodeString Vcode =""; for(inti =0; I <6; i++) {Vcode = Vcode + (int) (Math.random () *9); }returnVcode; }/** * Test * Method Description * @Discription: Extension description * @param args * @return void * @Author: Feizi * @Date: April 17, 2015 PM 7:26:36 * @ModifyUser: Feizi *
@ModifyDate: April 17, 2015 PM 7:26:36 * *
Public Static void Main(string[] args) {//System.out.println (Sendmsgutil.createrandomvcode ());//System.out.println ("&ecb=12". Substring (1));System.out.println (Sendmsg ("18123456789,15123456789","Dear user, your verification code is"+ Sendmsgutil.createrandomvcode () +", valid for 60 seconds, if in doubt please consult 400-069-2886 (Customer service telephone)" XXX Center ")); }}
Then execute, the general situation of the parameter is passed correctly, according to the specification of the interface document to operate, will send a successful, mobile phone can receive the verification code, and then may appear the problem is: Send the text message content may appear in Chinese garbled, and then will send unsuccessful, According to the requirements of the SMS platform for the corresponding code can be. It is usually UTF-8 encoded.
The full code has been uploaded to Csdn:
Http://download.csdn.net/my
Java implementation to send SMS verification verification code function