[1] Foreword
At present, the login method of most website apps is no longer single. It is undoubtedly the most convenient, fast and safe way to log in the account by obtaining the SMS verification code through the mobile phone.
There are many kinds of third-party SMS sending platforms. Each platform has its own advantages and disadvantages. When choosing, you can decide according to your specific actual situation. Today, LZ shares a better platform for code farmers to learn from. It's not advertising, but really easy to use.
LZ didn't know what SMS was before, but when watching a Daniel blogger share the springboot case last year, he mentioned that he continued to operate on Daniel's steps and code, and successfully completed the function of sending SMS. Recently, there happened to be a small demand for SMS verification. I went to Daniel's blog and was turned off. Fortunately, there is a backup code! Good things should be shared to meet the spirit of open source @#$!
[2] Platform opening
2.1: user registration
Baidu SMS, enter the official page and register the account;
Official website: http://sms.webchinese.cn/
2018-08-11_131642.png
2.2: log in to obtain necessary information
2018-08-11_131836.png
After logging in, the home page will display some information of the current user. Here we only focus on the number of short messages. Newly registered users will have 5 free test short messages. Here we need to remember 5 important information. Remember to keep it well
1: Account name 2: password 3: SMS secret key (signature verification secret key, encryption string with length of 20) 4: Binding mobile phone: 153 xxxxxxxx 5: Bind mailbox: xxxxxxxxx@qq.com
2018-08-11_132904.png
[3] Code implementation
1. Join dependency
<dependencies> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.0.1</version> </dependency> </dependencies>
2. SendMessageUtil utility class
/** * Created by lvfang on 2017/3/18. * * SMS sending tool */ public class SendMessageUtil { private static final String SMS_Url = "http://sms.webchinese.cn/web_api/"; /** * @param Uid SMS User id: lvfang123 * @param Key Interface secret key: SMS login (non login password) * @param sendPhoneNum SMS sending target number * @param desc SMS content * @return Integer(1: Success code, other failures (see note for details) */ public static Integer send(String Uid,String Key,String sendPhoneNum,String desc){ HttpClient client = new HttpClient(); PostMethod post = new PostMethod(SMS_Url); post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");// Set transcoding in header file //Set parameters NameValuePair[] data = { new NameValuePair("Uid", Uid), new NameValuePair("Key", Key),//Secret key new NameValuePair("smsMob", sendPhoneNum), new NameValuePair("smsText", desc) }; post.setRequestBody(data); try { client.executeMethod(post); } catch (Exception e) { e.printStackTrace(); } Header[] headers = post.getResponseHeaders(); int statusCode = post.getStatusCode(); System.out.println("statusCode:" + statusCode); for (Header h : headers) { System.out.println(h.toString()); } String result =""; try { result = new String(post.getResponseBodyAsString().getBytes("gbk")); } catch (Exception e) { e.printStackTrace(); } post.releaseConnection(); return Integer.parseInt(result); } /** * -1 There is no such user account -2 Incorrect interface key [View key] is not the account login password -21 MD5 Incorrect interface key encryption -3 Insufficient number of SMS messages -11 The user is disabled -14 Illegal characters in SMS content -4 Incorrect mobile number format -41 Mobile phone number is empty -42 SMS content is empty -51 The SMS signature format is incorrect. The interface signature format is: [signature content] -6 IP limit SMS sending quantity greater than 0 The above is a supplement */ public static String getMessage(Integer code){ String message; if(code > 0 ) { message = "SMS-f Sending succeeded! There's more text messages" + code + "strip"; }else if(code == -1){ message = "SMS-There is no such user account"; }else if(code == -2){ message = "SMS-Incorrect interface key"; }else if(code == -21){ message = "SMS-MD5 Incorrect interface key encryption"; }else if(code == -3){ message = "SMS-Insufficient number of SMS messages"; }else if(code == -11){ message = "SMS-The user is disabled"; }else if(code == -14){ message = "SMS-Illegal characters in SMS content"; }else if(code == -4){ message = "SMS-Incorrect mobile number format"; }else if(code == -41){ message = "SMS-Mobile phone number is empty"; }else if(code == -42){ message = "SMS-SMS content is empty"; }else if(code == -51){ message = "SMS-The SMS signature format is incorrect. The interface signature format is: [signature content]"; }else if(code == -6){ message = "SMS-IP limit"; }else{ message = "Other errors"; } return message; } /** * Randomly generate 6-bit verification code * @return */ public static String getRandomCode(Integer code){ Random random = new Random(); StringBuffer result= new StringBuffer(); for (int i=0;i<code;i++){ result.append(random.nextInt(10)); } return result.toString(); } }
3. Global test
/** * Randomly generate 6-bit verification code * @return */ public static String getRandomCode(){ Random random = new Random(); String result=""; for (int i=0;i<6;i++){ result+=random.nextInt(10); } return result; } /** * @author: lvfang * @mathName: testSendMessage * @parameter: nothing * @return value: * @throws null * @date 2018/8/11 * @desc SMS SMS test */ @Test public void testSendMessage(){ // SendMessageUtil.send("SMS account", "interface secret key", "target number", "sending content"); SendMessageUtil.send("lvfaxxxxxx","0b334927e1xxxxxxxxxx","153xxxxxxxxx","Verification Code:"+getRandomCode(6)); System.out.println(SendMessageUtil.getMessage(resultCode)); }
It should be noted here that if the current user is the test user (the user who has not paid the fee to bind the SMS template), the test content is limited, and the SMS is not opened and sent in time, the SMS sent by the program cannot be received immediately. For details, please refer to the notes on the SMS official website.
When new users use the interface test verification code, please do not enter irrelevant content information such as: test, please directly enter: Verification Code: xxxxxx, send.
4. Send response
Except for the 200 response code, there are other problems. Here is an API call interface document: http://sms.webchinese.cn/api.shtml
statusCode:200 Cache-Control: no-cache Content-Length: 2 Content-Type: text/html Expires: Fri, 10 Aug 2018 05:39:58 GMT Server: Microsoft-IIS/7.5 Set-Cookie: CHNET=Temp%5Fusername=201881113395989272; expires=Fri, 07-May-2021 05:39:58 GMT; path=/ Set-Cookie: ASPSESSIONIDAQBQTCCC=HDIPMIKCBOOOKHLDDPDKIAPI; path=/ X-Powered-By: ASP.NET Date: Sat, 11 Aug 2018 05:40:00 GMT ##Response code document /** * -1 There is no such user account -2 Incorrect interface key [View key] is not the account login password -21 MD5 Incorrect interface key encryption -3 Insufficient number of SMS messages -11 The user is disabled -14 Illegal characters in SMS content -4 Incorrect mobile number format -41 Mobile phone number is empty -42 SMS content is empty -51 The SMS signature format is incorrect. The interface signature format is: [signature content] -6 IP limit SMS sending quantity greater than 0 The above is a supplement */
5. Received text message
No accident, at this time, we will receive our own text messages
short message. png
6. Look back at the SMS log
Reprinted from: https://www.jianshu.com/p/fb1f90662a93