Java implements the SMS Verification Code sending Function

Source: Internet
Author: User
Tags sendmsg

Java implements the SMS Verification Code sending Function

In the past few days, the boss asked me to GET a function to send text message verification codes, using the SMS platform interface of Xinyi, and then using HttpClient in Java to simulate POST or GET requests (see the requirements of the SMS platform, generally, POST requests are used to call the interfaces provided by the SMS platform (follow the API specifications of the SMS platform ). Specific Code:

When using HttpClient, you must introduce:

Commons-httpclient-3.1.jar

This jar package,

Project Structure:

1. Create an Http simulated request tool class, and then write a POST method or GET method. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;"> /*** File Description * @ Description: Extension Description * @ Copyright: 2015 dreamtech.com.cn Inc. all right reserved * @ Version: V6.0 */package com. demo. util; import java. io. IOException; import java. util. map; import org. apache. commons. httpclient. httpClient; import org. apache. commons. httpclient. httpException; import org. apache. commons. httpclient. simpleHttpConnectionManager; import org. apache. commons. httpclient. methods. getMethod; import org. apache. commons. httpclient. methods. postMethod;/*** @ Author: feizi * @ Date: 9:26:34, January 1, April 17, 2015 * @ ModifyUser: feizi * @ ModifyDate: 9:26:34, January 1, April 17, 2015 * @ Version: v6.0 */public class HttpRequestUtil {/*** HttpClient simulate POST Request * method Description * @ Discription: Extension Description * @ param url * @ param params * @ return String * @ Author: feizi * @ Date: April 17, 2015 7:15:59 * @ ModifyUser: feizi * @ ModifyDate: April 17, 2015 7:15:59 */public static String postRequest (String url, Map Params) {// construct the HttpClient instance HttpClient httpClient = new HttpClient (); // create the POST Method Instance PostMethod postMethod = new PostMethod (url ); // set the request header information postMethod. setRequestHeader (Connection, close); // Add the parameter for (Map. entry Entry: params. entrySet () {postMethod. addParameter (entry. getKey (), entry. getValue ();} // use the default recovery policy provided by the system to set request retry processing. The default retry processing is used: Three httpClient requests. getParams (). setBooleanParameter (http. protocol. continue CT-continue, false); // receives the processing result String result = null; try {// execute the Http Post request httpClient.exe cuteMethod (postMethod); // return the processing result = postMethod. getResponseBodyAsString ();} catch (HttpException e) {// a fatal exception occurs. The Protocol is incorrect or the returned content is incorrect. System. out. println (check the entered URL !); E. printStackTrace ();} catch (IOException e) {// network exception System. out. println (network exception !); E. printStackTrace () ;}finally {// release the link postMethod. releaseConnection (); // close the HttpClient instance if (httpClient! = Null) {(SimpleHttpConnectionManager) httpClient. getHttpConnectionManager ()). shutdown (); httpClient = null;} return result;}/*** HttpClient simulate GET request * method Description * @ Discription: extension Description * @ param url * @ param params * @ return String * @ Author: feizi * @ Date: 7:15:28, January 1, April 17, 2015 * @ ModifyUser: feizi * @ ModifyDate: april 17, 2015 7:15:28 */public static String getRequest (String url, Map Params) {// construct HttpClient client = new HttpClient (); // The concatenation parameter String paramStr =; for (String key: params. keySet () {paramStr = paramStr + & + key + = + params. get (key);} paramStr = paramStr. substring (1); // create the GET method Instance GetMethod = new GetMethod (url +? + ParamStr); // receive the returned result String result = null; try {// execute the http get method Request client.exe cuteMethod (method); // return the processing result = method. getResponseBodyAsString ();} catch (HttpException e) {// a fatal exception may occur because the protocol is incorrect or the returned content is incorrect. System. out. println (check the entered URL !); E. printStackTrace ();} catch (IOException e) {// network exception System. out. println (network exception !); E. printStackTrace () ;}finally {// release the link method. releaseConnection (); // close the HttpClient instance if (client! = Null) {(SimpleHttpConnectionManager) client. getHttpConnectionManager (). shutdown (); client = null ;}} return result ;}}

2. Create a class, generate a verification code, and then pass the corresponding parameters (different SMS platform interfaces have different parameter requirements. This is generally available in the interface documentation provided by the SMS platform, directly read the document and follow the instructions)

/*** File Description * @ Description: Extension Description * @ Copyright: 2015 dreamtech.com.cn Inc. all right reserved * @ Version: V6.0 */package com. demo. util; import java.net. URLEncoder; import java. util. hashMap; import java. util. map;/*** @ Author: feizi * @ Date: 9:24:48, January 1, April 17, 2015 * @ ModifyUser: feizi * @ ModifyDate: 9:24:48, January 1, April 17, 2015 * @ Version: v6.0 */public class SendMsgUtil {/*** Method for sending SMS messages * @ Discription: extension Description * @ param phones * @ param content * @ return String * @ Author: feizi * @ Date: 7:18:08, January 1, April 17, 2015 * @ ModifyUser: feizi * @ ModifyDate: april 17, 2015 7:18:08 */@ SuppressWarnings (deprecation) public static String sendMsg (String phones, String content) {// SMS interface URL submission address String url = SMS interface URL submission address; Map
  
   
Params = new HashMap
   
    
(); Params. put (zh, user account); params. put (mm, user password); params. put (dxlbid, SMS category number); params. put (extno, extended number); // mobile phone number. Multiple numbers are separated by commas. put (hm, phones); // URLEncoder the text message to encode params. put (nr, URLEncoder. encode (content); return HttpRequestUtil. getRequest (url, params);}/*** randomly generated 6-digit random Verification Code * method Description * @ Discription: Extension Description * @ return String * @ Author: feizi * @ Date: April 17, 2015 7:19:02 * @ ModifyUser: feizi * @ ModifyDate: April 17, 2015 7:19:02 */public static String createRandomVcode () {// verification code String vcode =; for (int I = 0; I <6; I ++) {vcode = vcode + (int) (Math. random () * 9);} return vcode;}/*** test * method Description * @ Discription: Extension Description * @ param args * @ return void * @ Author: feizi * @ Date: April 17, 2015 7:26:36 * @ ModifyUser: feizi * @ ModifyDate: April 17, 2015 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 you have any concerns, please contact 400-069-2886 (Customer Service Hotline) [XXX center ]));}}
   
  

Then execute the following command. Generally, if the parameter is correctly transmitted and the operation is performed according to the specifications in the interface documentation, the verification code will be sent successfully, and the mobile phone will receive the verification code. Then the possible problem is: the text message may contain garbled Chinese characters, and cannot be sent successfully. encode the text message according to the requirements of the SMS platform. Generally, it is a UTF-8 code.

 

Related Article

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.