记录移动云MAS短信平台发送模板短信+获取回执状态
直接上代码,清晰明了(所有代码是写在⼀个⼯具类),也参考了⽹上⼀些博主的⽂章,⾮常感谢,以下内容只是记录平时做东西的内容,如有不正确的地⽅,欢迎指正。
PS:(写在前⾯)需要注意的⼏个点:
(1)需要先在mas平台上配置相关信息和接⼝(主页-管理-接⼝管理),https请求需要将短信平台上的短信接⼝创建为http协议模式,否则请求失败;http请求同理,只要短信接⼝的⽤户名、密码和协议对应正确,⼀般没有太⼤问题;
(2)如果是发送模板短信需要先申请短信模板,且传⼊参数必须和模板中变量所包含的类型⼀致,否则mas平台上数据校验⽆法通过(有条件还是获取⼀下回执报告,因为有时候即使响应状态返回success,最后没有收到短信,进⼊平台查看可以看到其实回执状态报错,并没有发送成功);
(3)如果短信模板中的变量长度不能满⾜需要,可以联系客户经理申请特殊变量,特殊变量⼀个模板中可以使⽤两次;
(4)https短信接⼝⽂档中写得很清楚:【请客户侧忽略证书校验,移动侧不提供证书秘钥】,所以在做post请求的时候⼀定要记得【忽略证书校验】,否则请求失败,返回值为空;http请求不⽤管。
(5)短信响应状态成功返回的是:{"msgGroup":"0906163415000000332084","rspcod":"success","success":true},有错误的时候是:{ "msgGroup": "", "rspcod": "InvalidUsrOrPwd",
"success": false}
(6)短信发送成功回执状态返回的是“[]”,有错误的时候是:
[{"errorCode":"CM:2007","mobile":"138********","msgGroup":"0906144506000001364223","receiveDate":"20210906244506","reportStatus":"CM:2007","submitDate":"20210906144506"}](7)上⾯说的其实平台上和提供的⽂档中基本都有说明,多看提供的接⼝⽂档,对实现⾃⼰想要的功能有帮助
1.SMSUtil.java
a.通过https请求发送模板短信(⽹上很多的都是⽤http⽅式请求,只不过获取回执状态需要⾃⼰搭建http服务,太菜了感觉⽐较⿇烦就采⽤了https 请求,可以⾃⼰主动获取回执报告,这个因⼈⽽异)
1/**
2 * 云MAS-发送模板短信(https)
3 *
4 * @param mobiles ⼿机号
5 * @param templateId 模板id
6 * @param param 参数列表
7 * @return
8 * @throws UnsupportedEncodingException
9*/
10public static AjaxResult sendTemplateSms(String mobiles, String templateId, List<String> param) throws UnsupportedEncodingException {
11 apId="xxx";//⽤户名
12 secretKey="xxx";//密码
13 ecName = "xxx"; //集团名称
14 sign = "xxx"; //⽹关签名编码
15 addSerial = ""; //拓展码填空
16 templateurl = "112.35.10.201:28888/sms/tmpsubmit";//和http请求地址不⼀样 112.35.1.155:1992/sms/tmpsubmit
17
18//--start--
19 String params = null;
20if(param != null){
21 params = Array());
22 }else{
23 params = "[]";
24 }
25 SendRequest submitReq = new SendRequest();
26 submitReq.setApId(apId);
27 submitReq.setEcName(ecName);
28 submitReq.setSecretKey(secretKey);
29 submitReq.setParams(params);
30 submitReq.setMobiles(mobiles);
31 submitReq.setSign(sign);
32 submitReq.setAddSerial(addSerial);
33 submitReq.setTemplateId(templateId);
34
35 StringBuffer stringBuffer = new StringBuffer();
36 stringBuffer.EcName());
37 stringBuffer.ApId());
38 stringBuffer.SecretKey());
39 stringBuffer.TemplateId());
40 stringBuffer.Mobiles());
41 stringBuffer.Params());
42 stringBuffer.Sign());
43 stringBuffer.AddSerial());
44 String encode = "";
45try {
46//接⼝要求参数为MD5加密后的值
47 submitReq.setMac(Md5Utils.String()));
48 String reqText = JSONString(submitReq);
49//base64编码
50 encode =new Bytes("UTF-8"))); //有中⽂时使⽤UTF-8
51 } catch (UnsupportedEncodingException e1) {
52 e1.printStackTrace();
53 }
54 String resStr = "";
55try {
56 resStr = new String(HttpUtils.sendPostTrust(templateurl,encode,"utf-8","POST"));//https链接
57 } catch (Exception e) {
58 e.printStackTrace();
59 }
60//--end--
61
62//响应状态、回执状态处理--start--
63 SendResponse sendRes = JSON.parseObject(resStr,SendResponse.class);//响应状态
64 String receiptReport=getReport(apId,secretKey,ecName);//回执状态
66 String errorCode="";
67if(receiptReport!="[]"){
68 JSONArray jsonArray= JSONArray.parseArray(receiptReport);
ains("errorCode")){
70for(int index=0;index<jsonArray.size();index++){
71 JSONObject jsonObject = JSONObject(index);
String("mobile").equals(mobiles)) {
73 ("errorCode").toString();
74 }
75 }
76 }
77 }
78else{
79 errorCode="";
80 }
Success() && !"".MsgGroup()) && "success".Rspcod())){ 82switch (Rspcod()) {
83case "success":
84if (errorCode != null && errorCode != "") {
(11, "其他错误:" + errorCode+",请联系管理员!");
86 } else {
(0, "发送成功");
88 }
89case "IllegalMac ":
(1, "mac校验不通过");
91case "IllegalSignId ":
(2, "⽆效的签名编码");
93case "InvalidMessage ":
(3, "⾮法消息,请求数据解析失败");
95case "InvalidUsrOrPwd ":
(4, "⾮法⽤户名/密码");
97case "NoSignId ":
(5, "未匹配到对应的签名信息");
99case "TooManyMobiles ":
(6, "⼿机号数量超限,应≤5000");
101default:
(-1, "系统异常");
103 }
104 }
105else{
(-2,"请求异常");
107 }
108//响应状态、回执状态处理--end--
109 }
110
111//获取状态报告
112public static String getReport(String apId,String secretKey,String ecName){
113 String reporturl = "112.35.10.201:28888/sms/report";
114
115 SendRequest submitReq = new SendRequest();
116 submitReq.setApId(apId);
117 submitReq.setEcName(ecName);
118 submitReq.setSecretKey(secretKey);
119
120 StringBuffer stringBuffer = new StringBuffer();
121 stringBuffer.EcName());
122 stringBuffer.ApId());
123 stringBuffer.SecretKey());
124 stringBuffer.TemplateId());
125 String encode = "";
126try {
127//接⼝要求参数为MD5加密后的值
128 submitReq.setMac(Md5Utils.String()));
129 String reqText = JSONString(submitReq);
130//base64编码
131 encode =new Bytes("UTF-8"))); //有中⽂时使⽤UTF-8 132 } catch (UnsupportedEncodingException e1) {
133 e1.printStackTrace();
134 }
135 String receiptReport = "";
136try {
137 receiptReport = new String(HttpUtils.sendPostTrust(reporturl,encode,"utf-8","POST"));//https链接
138
139 } catch (Exception e) {
140 e.printStackTrace();
141 }
142return receiptReport;
143 }
2.HttpUtils.java---->sendPostTrust
1//添加信任主机
2private static void trustAllHosts() {
3// 创建不验证证书链的信任管理器这⾥使⽤的是x509证书
4 TrustManager[] trustAllCerts = new TrustManager[]{new TrustAnyTrustManager() {
5public X509Certificate[] getAcceptedIssuers() {
6return new X509Certificate[]{};
7 }
8
9public void checkClientTrusted(X509Certificate[] chain, String authType) {
10 }
11
12public void checkServerTrusted(X509Certificate[] chain, String authType) {
13 }
14 }};
15// 安装所有信任的信任管理器
16try {
17 SSLContext sc = Instance("TLS");
18 sc.init(null, trustAllCerts, new java.security.SecureRandom());
19//HttpsURLConnection通过SSLSocket来建⽴与HTTPS的安全连接,SSLSocket对象是由SSLSocketFactory⽣成的。
20 HttpsURLConnection.SocketFactory());
21 } catch (Exception e) {
22 e.printStackTrace();
23 }
24 }
25
26/**
27 * 发送post 数据并忽略证书校验
28 * @param urls
29 * @return
30*/
31public static String sendPostTrust(String urls, String param, String contentType, String method) {
32 StringBuffer sb=new StringBuffer();
33 DataOutputStream out = null;
34 BufferedReader responseReader = null;
35 InputStream in1 = null;
36try {
37 trustAllHosts();
38// 创建url资源
39 URL url = new URL(urls);
40// 建⽴http连接
41 HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
42 conn.setHostnameVerifier(DO_NOT_VERIFY);
43// 设置不⽤缓存
44 conn.setUseCaches(false);
45// 设置允许输出
46 conn.setDoOutput(true);
47// 设置允许输⼊
48 conn.setDoInput(true);
49// 设置传递⽅式
50 conn.setRequestMethod(method);
51//System.out.RequestMethod());
52// 设置维持长连接
53 conn.setRequestProperty("Connection", "Keep-Alive");
54// 设置⽂件字符集:
55 conn.setRequestProperty("Charset", "UTF-8");
56// 转换为字节数组
57// byte[] data = (param).getBytes();
58//// 设置⽂件长度
error parse new59// conn.setRequestProperty("Content-Length", String.valueOf(data.length));
60// 设置⽂件类型:
61 conn.setRequestProperty("Content-Type", contentType);
62// 开始连接请求
63 t();
64 out = new OutputStream());
65// 写⼊请求的字符串
66 out.writeBytes(param);
67 out.flush();
68
69//System.out.ResponseCode());
70
71// 请求返回的状态
72if (HttpURLConnection.HTTP_OK == ResponseCode()) {
73 System.out.println("连接成功");
74// 请求返回的数据
75 in1 = InputStream();
76 String readLine;
77 responseReader = new BufferedReader(new InputStreamReader(in1)); 78while((adLine()) != null){
79 sb.append(readLine).append("\n");
80 }
81 } else {
82 System.out.println("error++");
83 }
84 } catch (Exception e) {
85
86 } finally {
87try {
88if (null != responseReader)
89 responseReader.close();
90if (null != in1)
91 in1.close();
92 } catch(Exception e) {}
93try {
94 out.close();
95 } catch(Exception e) {}
96 }
97
String();
99
100 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论