说明 项目是springboot框架
java" id="highlighter_119749">
1
2
3
|
chuanglan.requesturl= chuanglan.account= chuanglan.pswd= |
配置文件
具体值 查看官网 位置查看截图 红框已经标红
2.读取配置文件类
3.发送数据request实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
public class smsvariablerequest { private string account; private string password; private string msg; private string params; private string sendtime; private string report; private string extend; private string uid; private string msgid; private string failnum; private string successnum; private string phone; public smsvariablerequest() { } public smsvariablerequest(string account, string password, string msg, string params) { this .account = account; this .password = password; this .msg = msg; this .params = params; } public smsvariablerequest(string account, string password, string msg, string params, string report,string phone) { this .account = account; this .password = password; this .msg = msg; this .params = params; this .report = report; this .phone = phone; } public string getaccount() { return this .account; } public void setaccount(string account) { this .account = account; } public string getpassword() { return this .password; } public void setpassword(string password) { this .password = password; } public string getmsg() { return this .msg; } public void setmsg(string msg) { this .msg = msg; } public string getsendtime() { return this .sendtime; } public void setsendtime(string sendtime) { this .sendtime = sendtime; } public string getreport() { return this .report; } public void setreport(string report) { this .report = report; } public string getextend() { return this .extend; } public void setextend(string extend) { this .extend = extend; } public string getuid() { return this .uid; } public void setuid(string uid) { this .uid = uid; } public string getparams() { return this .params; } public void setparams(string params) { this .params = params; } public string getmsgid() { return msgid; } public void setmsgid(string msgid) { this .msgid = msgid; } public string getfailnum() { return failnum; } public void setfailnum(string failnum) { this .failnum = failnum; } public string getsuccessnum() { return successnum; } public void setsuccessnum(string successnum) { this .successnum = successnum; } public string getphone() { return phone; } public void setphone(string phone) { this .phone = phone; } } |
4.接收数据response实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
public class smsvariableresponse { private string time; private string msgid; private string errormsg; private string failnum; private string successnum; private string code; public string gettime() { return this .time; } public void settime(string time) { this .time = time; } public string getmsgid() { return this .msgid; } public void setmsgid(string msgid) { this .msgid = msgid; } public string geterrormsg() { return this .errormsg; } public void seterrormsg(string errormsg) { this .errormsg = errormsg; } public string getcode() { return this .code; } public void setcode(string code) { this .code = code; } public string getfailnum() { return this .failnum; } public void setfailnum(string failnum) { this .failnum = failnum; } public string getsuccessnum() { return this .successnum; } public void setsuccessnum(string successnum) { this .successnum = successnum; } public string tostring() { return "smsvarableresponse [time=" + this .time + ", msgid=" + this .msgid + ", errormsg=" + this .errormsg + ", failnum=" + this .failnum + ", successnum=" + this .successnum + ", code=" + this .code + "]" ; } |
5.创蓝短信发送请求工具类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
public class chuanglansmsutil { public static string sendsmsbypost(string path, string postcontent) { url url = null ; try { url = new url(path); httpurlconnection httpurlconnection = (httpurlconnection)url.openconnection(); httpurlconnection.setrequestmethod( "post" ); httpurlconnection.setconnecttimeout( 10000 ); httpurlconnection.setreadtimeout( 2000 ); httpurlconnection.setdooutput( true ); httpurlconnection.setdoinput( true ); httpurlconnection.setrequestproperty( "charset" , "utf-8" ); httpurlconnection.setrequestproperty( "content-type" , "application/json" ); httpurlconnection.connect(); outputstream os = httpurlconnection.getoutputstream(); os.write(postcontent.getbytes( "utf-8" )); os.flush(); stringbuilder sb = new stringbuilder(); int httprspcode = httpurlconnection.getresponsecode(); if (httprspcode == 200 ) { bufferedreader br = new bufferedreader( new inputstreamreader(httpurlconnection.getinputstream(), "utf-8" )); string line = null ; while ((line = br.readline()) != null ) { sb.append(line); } br.close(); return sb.tostring(); } } catch (exception e) { e.printstacktrace(); } return null ; } } |
6.发送短信验证码具体代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@override public map<string, object> send(string content, string mobilenumber) { string report = "true" ; content= "【】您的验证码是:" +content; smsvariablerequest smsvariablerequest = new smsvariablerequest(chuanglansmsconstants.getaccount(), chuanglansmsconstants.getpswd(), content, null , report,mobilenumber); string requestjson = json.tojsonstring(smsvariablerequest); string response = chuanglansmsutil.sendsmsbypost(chuanglansmsconstants.getrequesturl(), requestjson); smsvariableresponse smsvariableresponse = (smsvariableresponse)json.parseobject(response, smsvariableresponse. class ); system.out.println( "response tostring is : " + smsvariableresponse); if ( null !=smsvariableresponse&&! "0" .equals(smsvariableresponse.getcode())){ if (log.isinfoenabled()){ log.info(smsvariableresponse); } } return null ; } |
7.注意事项
(1)注意短信签名 【xxxx】 必须是 创蓝短信审核通过的签名 如果是测试 可以使用【253云通讯】默认的 没有问题
(2)创蓝短信返回的错误并不是很明确 不要只注意控制台创蓝返回的错误 注意看创蓝短信在线api
(3)注意看在线demo不建议看离线demo 会有在线demo已经更新离线demo还未更新的情况
(4)如果需要判断验证码是否发送成功 直接"0".equals(smsvariableresponse.getcode()) “0”即发送成功
以上这篇java接入创蓝253短信验证码的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/weitaming/archive/2018/01/04/8195523.html