Click on the top [ full-stack developer community ] → upper right corner [...] → [set to star ⭐ ]
Hand machine SMS verification now in various systems can be said is a very common, this may be the convenience and safety considerations, so it is widely used, this article on to a short message interface instance, to explain how use SMS interface that allows small partners also experience it, Lala, function is simple.
First, the preliminary work
First, we need to select a company messaging interface, and then to register and get an ID number, etc., then you can create a formal business of our SMS. Below an SMS interface as an example to explain.
1.1 Registration
http://www.miaodiyun.com/index.html (look for the personal use of which platform, this is just an example)
1.2, and acquired ACCOUNT SID AUTH TOKEN
1.3, create SMS templates
As shown above, click Configuration Manager , and then enter the message template , and then click New Template , created your SMS templates .
Here are my template as a reference.
Note: created above text template information, you need to use in the code, and some need to be consistent, otherwise, there will be an exception.
For example, the above message template information should read: "[Technology] Ouyang login verification code: {1}, if not I operate, please ignore this message," {1}
as a placeholder, your text message verification code.
Well, Once you have these ready, you can begin to send text messages.
Second, specific code
config.java:
Here we need to modify the registration we get to ACCOUNT SID
and AUTH TOKEN
.
1/**
2 * 系统常量
3 */
4public class Config
5{
6 /**
7 * url前半部分
8 */
9 public static final String BASE_URL = "https://api.miaodiyun.com/20150822";
10 /**
11 * 开发者注册后系统自动生成的账号,可在官网登录后查看
12 */
13 public static final String ACCOUNT_SID = "aac6e373c7534007bf47648ba34ba2f1";
14 /**
15 * 开发者注册后系统自动生成的TOKEN,可在官网登录后查看
16 */
17 public static final String AUTH_TOKEN = "47605360a97a4f81bcd576e8e0645edf";
18 /**
19 * 响应数据类型, JSON或XML
20 */
21 public static final String RESP_DATA_TYPE = "json";
22}
HttpUtil.java (http request tool):
1import java.io.BufferedReader;
2import java.io.IOException;
3import java.io.InputStreamReader;
4import java.io.OutputStreamWriter;
5import java.net.URL;
6import java.net.URLConnection;
7import java.text.SimpleDateFormat;
8import java.util.Date;
9import org.apache.commons.codec.digest.DigestUtils;
10/**
11 * http请求工具
12 */
13public class HttpUtil
14{
15 /**
16 * 构造通用参数timestamp、sig和respDataType
17 *
18 * @return
19 */
20 public static String createCommonParam()
21 {
22 // 时间戳
23 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
24 String timestamp = sdf.format(new Date());
25 // 签名
26 String sig = DigestUtils.md5Hex(Config.ACCOUNT_SID + Config.AUTH_TOKEN + timestamp);
27 return "×tamp=" + timestamp + "&sig=" + sig + "&respDataType=" + Config.RESP_DATA_TYPE;
28 }
29 /**
30 * post请求
31 *
32 * @param url
33 * 功能和操作
34 * @param body
35 * 要post的数据
36 * @return
37 * @throws IOException
38 */
39 public static String post(String url, String body)
40 {
41 System.out.println("url:" + System.lineSeparator() + url);
42 System.out.println("body:" + System.lineSeparator() + body);
43 String result = "";
44 try
45 {
46 OutputStreamWriter out = null;
47 BufferedReader in = null;
48 URL realUrl = new URL(url);
49 URLConnection conn = realUrl.openConnection();
50 // 设置连接参数
51 conn.setDoOutput(true);
52 conn.setDoInput(true);
53 conn.setConnectTimeout(5000);
54 conn.setReadTimeout(20000);
55 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
56 // 提交数据
57 out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
58 out.write(body);
59 out.flush();
60 // 读取返回数据
61 in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
62 String line = "";
63 boolean firstLine = true; // 读第一行不加换行符
64 while ((line = in.readLine()) != null)
65 {
66 if (firstLine)
67 {
68 firstLine = false;
69 } else
70 {
71 result += System.lineSeparator();
72 }
73 result += line;
74 }
75 } catch (Exception e)
76 {
77 e.printStackTrace();
78 }
79 return result;
80 }
81 /**
82 * 回调测试工具方法
83 *
84 * @param url
85 * @param reqStr
86 * @return
87 */
88 public static String postHuiDiao(String url, String body)
89 {
90 String result = "";
91 try
92 {
93 OutputStreamWriter out = null;
94 BufferedReader in = null;
95 URL realUrl = new URL(url);
96 URLConnection conn = realUrl.openConnection();
97 // 设置连接参数
98 conn.setDoOutput(true);
99 conn.setDoInput(true);
100 conn.setConnectTimeout(5000);
101 conn.setReadTimeout(20000);
102 // 提交数据
103 out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
104 out.write(body);
105 out.flush();
106 // 读取返回数据
107 in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
108 String line = "";
109 boolean firstLine = true; // 读第一行不加换行符
110 while ((line = in.readLine()) != null)
111 {
112 if (firstLine)
113 {
114 firstLine = false;
115 } else
116 {
117 result += System.lineSeparator();
118 }
119 result += line;
120 }
121 } catch (Exception e)
122 {
123 e.printStackTrace();
124 }
125 return result;
126 }
127}
SMS verification code to inform the most important interfaces :()
Modify smsContent SMS templates Note: Be sure to be consistent.
1import java.net.URLEncoder;
2import com.miaodiyun.httpApiDemo.common.Config;
3import com.miaodiyun.httpApiDemo.common.HttpUtil;
4/**
5 * 验证码通知短信接口
6 *
7 * @ClassName: IndustrySMS
8 * @Description: 验证码通知短信接口
9 *
10 */
11public class IndustrySMS
12{
13 private static String operation = "/industrySMS/sendSMS";
14 private static String accountSid = Config.ACCOUNT_SID;
15 private static String to = "13767441759";
16 private static String code = smsCode();
17// 登录验证码:{1},如非本人操作,请忽略此短信。
18 private static String smsContent = "【欧阳科技】登录验证码:"+code+",如非本人操作,请忽略此短信。";
19 /**
20 * 验证码通知短信
21 */
22 public static void execute()
23 {
24 String tmpSmsContent = null;
25 try{
26 tmpSmsContent = URLEncoder.encode(smsContent, "UTF-8");
27 }catch(Exception e){
28 }
29 String url = Config.BASE_URL + operation;
30 String body = "accountSid=" + accountSid + "&to=" + to + "&smsContent=" + tmpSmsContent
31 + HttpUtil.createCommonParam();
32 // 提交请求
33 String result = HttpUtil.post(url, body);
34 System.out.println("result:" + System.lineSeparator() + result);
35 }
36 //创建验证码
37 public static String smsCode(){
38 String random=(int)((Math.random()*9+1)*100000)+"";
39 System.out.println("验证码:"+random);
40 return random;
41 }
42}
These are the main categories above, there are other classes given source code at the end of the article.
Third, the SMS verification test
Other specific functions, such as voice messaging, etc., view the following source code!
Source code download
https://download.csdn.net/download/sihai12345/10472391
How to buy : Click applet or direct purchase or read the original to learn more. . .