Preface
Recently, I have been working on a remote intelligent water meter management system. There is a function of this process that can be registered using mobile phones on the registration page. I have been searching for a long time before I roughly understand the implementation process of mobile phone verification code. I will share it with you today. I won’t say much below, let’s take a look at the detailed introduction together.
SMS verification code implementation process
1. Construct mobile phone verification code: Use the random number required by the random object to generate as the verification code, such as 4-bit verification code: a random number between 1000 and 9999;
2. Use the interface to send mobile phone number and verification code data to the SMS platform, and then the SMS platform sends the verification code to the formulated mobile phone number. The interface parameters generally include: target mobile phone number, random verification code (or including expiration time), platform interface address, and platform password;
3. Save the information returned by the interface (usually json text data, and then needs to be converted to json object format);
4. Store the mobile phone number-verification code and operation time in the Session for later verification;
5. Receive verification codes and other data filled in by the user;
6. Compare whether the submitted verification code is consistent with the verification code in the Session, and determine whether the submission action is within the validity period;
7. The verification code is correct and within the validity period, the request is passed to process the corresponding business.
Today we will implement the first 4 steps:
To simplify the process, we use fixed mobile phone verification code instead of randomly generated numbers as verification code
Here we have a front-end page and a back-end logic processing page. The following are two items to briefly explain:
Front-end page
<body> <h1> <% Send send = new Send(); String PostData = "account=JamesXT&password=18770918982lkx&mobile=18770918982&content="+java.net.URLEncoder.encode("Your order code: 4557. If you need help, please contact customer service.","utf-8"); //out.println(PostData); String ret = Send.SMS(PostData, "http://sms.106jiekou.com/utf8/sms.aspx"); out.println(ret); //Please deserialize the returned string yourself and implement your own logic %> </h1> </body>PostData: It is the pending data that you want to submit to the SMS service platform. It generally includes the user account name and password of the SMS service platform registrant, as well as the complete content of whom you want to send the SMS verification code and the information to which the SMS verification code belongs.
String ret = Send.SMS(PostData, http://sms.106jiekou.com/utf8/sms.aspx);
This is to bind the data to the website of the SMS service platform, that is, which platform to deliver the submitted data to for processing.
Background logic processing class:
public class Send { //There are two parameters in the SMS method. This parameter is determined by the PostData submitted by the foreground page and the SMS service address bound to the former. Public static String SMS(String postData, String postUrl) { try { //Send POST request URL url = new URL(postUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setUseCaches(false); conn.setDoOutput(true); conn.setRequestProperty("Content-Length", "" + postData.length()); OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); //Put the data submitted by the foreground into the output stream, that is, send the service request to the SMS service platform out.write(postData); out.flush(); out.close(); //Get the response status code if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { System.out.println("connect failed!"); return ""; } //Get the response content String line, result = ""; BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); while ((line = in.readLine()) != null) { result += line + "/n"; } in.close(); return result; } catch (IOException e) { e.printStackTrace(System.out); } return ""; }} Finally, the Demo source code address is attached: http://xiazai.VeVB.COM/201712/yuanma/MobileRegist(VeVB.COM).rar
The SMS service provider address used in this demo is: http://www.106jiekou.com/login/?ReturnUrl=/member/trigger/templates/
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.