点击上方[全栈开发者社区]→右上角[...]→[设为星标⭐]
手机短信验证现在在各种系统可以说都是用的非常普遍的,这个可能是方便和安全性的考虑,所以才广泛的使用,这篇文章就以一个短信接口的实例,来讲解一下怎么使用短信接口,让小伙伴们也体验一下吧,啦啦,功能虽然简单。
一、前期工作
首先,我们需要选定一家短信接口的公司,然后去注册和获取一系列的ID等,然后就可以正式的创建我们的短信业务了。下面以某个短信接口为例讲解。
1.1、注册
http://www.miaodiyun.com/index.html(对于用哪个平台的看个人,这个只是实例)
1.2、获取到ACCOUNT SID和AUTH TOKEN
1.3、创建短信模板
如上图,点击配置管理,然后进入到短信模板,再点击新建模板,创建好你的短信模板。
下面给出我的模板作为参考。
注意:上面创建的短信模板的信息,需要在代码中用到,并且一定需要保持一致,否则,会出现异常。
例如,上面的短信模板的信息应为:“【欧阳科技】登录验证码:{1},如非本人操作,请忽略此短信。”,{1}
为占位符,是你的短信验证码。
好了,有了这些准备之后,就可以开始发短信了。
二、具体代码
config.java:
这里我们需要修改我们注册时获取到的ACCOUNT SID
和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请求工具):
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}
验证码通知短信接口:(最重要)
修改smsContent短信模板注意:一定要保持一致。
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}
上面这些是主要的类,还有其他的类在文章末尾给出源代码。
三、手机短信验证测试
其他的具体的功能,比如,语音短信等,查看下面的源码!
源代码下载
https://download.csdn.net/download/sihai12345/10472391
如何购买:点击小程序直接购买或或阅读原文了解更多。。。
该书参与了当当“程序员节”活动,每满100减50哟~