2023年7月31日发(作者:)
记录移动云MAS短信平台发送模板短信+获取回执状态直接上代码,清晰明了(所有发送短信代码是写在⼀个⼯具类),也参考了⽹上⼀些博主的⽂章,⾮常感谢,以下内容只是记录平时做东西的内容,如有不正确的地⽅,欢迎指正。PS:(写在前⾯)需要注意的⼏个点:(1)需要先在mas平台上配置相关信息和接⼝(主页-管理-接⼝管理),https请求需要将短信平台上的短信接⼝创建为http协议模式,否则请求失败;http请求同理,只要短信接⼝的⽤户名、密码和协议对应正确,⼀般没有太⼤问题;(2)如果是发送模板短信需要先申请短信模板,且传⼊参数必须和模板中变量所包含的类型⼀致,否则mas平台上数据校验⽆法通过(有条件还是获取⼀下回执报告,因为有时候即使响应状态返回success,最后没有收到短信,进⼊平台查看可以看到其实回执状态报错,并没有发送成功);(3)如果短信模板中的变量长度不能满⾜需要,可以联系客户经理申请特殊变量,特殊变量⼀个模板中可以使⽤两次; (4)https短信接⼝⽂档中写得很清楚:【请客户侧忽略证书校验,移动侧不提供证书秘钥】,所以在做post请求的时候⼀定要记得【忽略证书校验】,否则请求失败,返回值为空;http请求不⽤管。(5)短信响应状态成功返回的是:{"msgGroup":"","rspcod":"success","success":true},有错误的时候是:{ "msgGroup": "", "rspcod": "InvalidUsrOrPwd",
"success": false}(6)短信发送成功回执状态返回的是“[]”,有错误的时候是:[{"errorCode":"CM:2007","mobile":"","msgGroup":"","receiveDate":"26","reportStatus":"CM:2007","submitDate":"26"}](7)上⾯说的其实平台上和提供的⽂档中基本都有说明,多看提供的接⼝⽂档,对实现⾃⼰想要的功能有帮助
.通过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 */ 10 public static AjaxResult sendTemplateSms(String mobiles, String templateId, List
18 //发送短信--start-- 19 String params = null; 20 if(param != null){ 21 params = String(y()); 22 }else{ 23 params = "[]"; 24 } 25 SendRequest submitReq = new SendRequest(); 26 d(apId); 27 ame(ecName); 28 retKey(secretKey); 29 ams(params); 30 iles(mobiles); 31 n(sign); 32 Serial(addSerial); 33 plateId(templateId); 34
35 StringBuffer stringBuffer = new StringBuffer(); 36 (ame()); 37 (d()); 38 (retKey()); 39 (plateId()); 40 (iles()); 41 (ams()); 42 (n()); 43 (Serial()); 44 String encode = ""; 45 try { 46 //接⼝要求参数为MD5加密后的值 47 ((ng())); 48 String reqText = String(submitReq); 49 //base64编码 50 encode =new String(Base64Chunked(es("UTF-8"))); //有中⽂时使⽤UTF-8 51 } catch (UnsupportedEncodingException e1) { 52 tackTrace(); 53 } 54 String resStr = ""; 55 try { 56 resStr = new String(stTrust(templateurl,encode,"utf-8","POST"));//https链接 57 } catch (Exception e) { 58 tackTrace(); 59 } 60 //发送短信--end-- 61
62 //响应状态、回执状态处理--start-- 63 SendResponse sendRes = bject(resStr,);//响应状态 64 String receiptReport=getReport(apId,secretKey,ecName);//回执状态 66 String errorCode=""; 67 if(receiptReport!="[]"){ 68 JSONArray jsonArray= rray(receiptReport); 69 if(ns("errorCode")){ 70 for(int index=0;index<();index++){ 71 JSONObject jsonObject = NObject(index); 72 if(ing("mobile").equals(mobiles)) { 73 errorCode=("errorCode").toString(); 74 } 75 } 76 } 77 } 78 else{ 79 errorCode=""; 80 } 81 if(cess() && !"".equals(Group()) && "success".equals(cod())){ 82 switch (cod()) { 83 case "success": 84 if (errorCode != null && errorCode != "") { 85 return (11, "其他错误:" + errorCode+",请联系管理员!"); 86 } else { 87 return (0, "发送成功"); 88 } 89 case "IllegalMac ": 90 return (1, "mac校验不通过"); 91 case "IllegalSignId ": 92 return (2, "⽆效的签名编码"); 93 case "InvalidMessage ": 94 return (3, "⾮法消息,请求数据解析失败"); 95 case "InvalidUsrOrPwd ": 96 return (4, "⾮法⽤户名/密码"); 97 case "NoSignId ": 98 return (5, "未匹配到对应的签名信息"); 99 case "TooManyMobiles ":100 return (6, "⼿机号数量超限,应≤5000");101 default:102 return (-1, "系统异常");103 }104 }105 else{106 return (-2,"请求异常");107 }108 //响应状态、回执状态处理--end--109 }110
111 //获取状态报告112 public 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 d(apId);117 ame(ecName);118 retKey(secretKey);119
120 StringBuffer stringBuffer = new StringBuffer();121 (ame());122 (d());123 (retKey());124 (plateId());125 String encode = "";126 try {127 //接⼝要求参数为MD5加密后的值128 ((ng()));129 String reqText = String(submitReq);130 //base64编码131 encode =new String(Base64Chunked(es("UTF-8"))); //有中⽂时使⽤UTF-8132 } catch (UnsupportedEncodingException e1) {133 tackTrace();134 }135 String receiptReport = "";136 try {137 receiptReport = new String(stTrust(reporturl,encode,"utf-8","POST"));//https链接138 139 } catch (Exception e) {140 tackTrace();141 }142 return receiptReport;143 }
---->sendPostTrust 1 //添加信任主机 2 private static void trustAllHosts() { 3 // 创建不验证证书链的信任管理器 这⾥使⽤的是x509证书 4 TrustManager[] trustAllCerts = new TrustManager[]{new TrustAnyTrustManager() { 5 public .X509Certificate[] getAcceptedIssuers() { 6 return new .X509Certificate[]{}; 7 } 8
9 public void checkClientTrusted(X509Certificate[] chain, String authType) { 10 } 11
12 public void checkServerTrusted(X509Certificate[] chain, String authType) { 13 } 14 }}; 15 // 安装所有信任的信任管理器 16 try { 17 SSLContext sc = tance("TLS"); 18 (null, trustAllCerts, new Random()); 19 //HttpsURLConnection通过SSLSocket来建⽴与HTTPS的安全连接,SSLSocket对象是由SSLSocketFactory⽣成的。 20 aultSSLSocketFactory(ketFactory()); 21 } catch (Exception e) { 22 tackTrace(); 23 } 24 } 25
26 /** 27 * 发送post 数据并忽略证书校验 28 * @param urls 29 * @return 30 */ 31 public 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; 36 try { 37 trustAllHosts(); 38 // 创建url资源 39 URL url = new URL(urls); 40 // 建⽴http连接 41 HttpsURLConnection conn = (HttpsURLConnection) nnection(); 42 tnameVerifier(DO_NOT_VERIFY); 43 // 设置不⽤缓存 44 Caches(false); 45 // 设置允许输出 46 utput(true); 47 // 设置允许输⼊ 48 nput(true); 49 // 设置传递⽅式 50 uestMethod(method); 51 //n(uestMethod()); 52 // 设置维持长连接 53 uestProperty("Connection", "Keep-Alive"); 54 // 设置⽂件字符集: 55 uestProperty("Charset", "UTF-8"); 56 // 转换为字节数组 57 // byte[] data = (param).getBytes(); 58 // // 设置⽂件长度 59 // uestProperty("Content-Length", f()); 60 // 设置⽂件类型: 61 uestProperty("Content-Type", contentType); 62 // 开始连接请求 63 t(); 64 out = new DataOutputStream(putStream()); 65 // 写⼊请求的字符串 66 ytes(param); 67 (); 68
69 //n(ponseCode()); 70
71 // 请求返回的状态 72 if (_OK == ponseCode()) { 73 n("连接成功"); 74 // 请求返回的数据 75 in1 = utStream(); 76 String readLine; 77 responseReader = new BufferedReader(new InputStreamReader(in1)); 78 while((readLine=ne()) != null){ 79 (readLine).append("n"); 80 } 81 } else { 82 n("error++"); 83 } 84 } catch (Exception e) { 85
86 } finally { 87 try { 88 if (null != responseReader) 89 (); 90 if (null != in1) 91 (); 92 } catch(Exception e) {} 93 try { 94 (); 95 } catch(Exception e) {} 96 } 97
98 return ng(); 99
100 }
2023年7月31日发(作者:)
记录移动云MAS短信平台发送模板短信+获取回执状态直接上代码,清晰明了(所有发送短信代码是写在⼀个⼯具类),也参考了⽹上⼀些博主的⽂章,⾮常感谢,以下内容只是记录平时做东西的内容,如有不正确的地⽅,欢迎指正。PS:(写在前⾯)需要注意的⼏个点:(1)需要先在mas平台上配置相关信息和接⼝(主页-管理-接⼝管理),https请求需要将短信平台上的短信接⼝创建为http协议模式,否则请求失败;http请求同理,只要短信接⼝的⽤户名、密码和协议对应正确,⼀般没有太⼤问题;(2)如果是发送模板短信需要先申请短信模板,且传⼊参数必须和模板中变量所包含的类型⼀致,否则mas平台上数据校验⽆法通过(有条件还是获取⼀下回执报告,因为有时候即使响应状态返回success,最后没有收到短信,进⼊平台查看可以看到其实回执状态报错,并没有发送成功);(3)如果短信模板中的变量长度不能满⾜需要,可以联系客户经理申请特殊变量,特殊变量⼀个模板中可以使⽤两次; (4)https短信接⼝⽂档中写得很清楚:【请客户侧忽略证书校验,移动侧不提供证书秘钥】,所以在做post请求的时候⼀定要记得【忽略证书校验】,否则请求失败,返回值为空;http请求不⽤管。(5)短信响应状态成功返回的是:{"msgGroup":"","rspcod":"success","success":true},有错误的时候是:{ "msgGroup": "", "rspcod": "InvalidUsrOrPwd",
"success": false}(6)短信发送成功回执状态返回的是“[]”,有错误的时候是:[{"errorCode":"CM:2007","mobile":"","msgGroup":"","receiveDate":"26","reportStatus":"CM:2007","submitDate":"26"}](7)上⾯说的其实平台上和提供的⽂档中基本都有说明,多看提供的接⼝⽂档,对实现⾃⼰想要的功能有帮助
.通过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 */ 10 public static AjaxResult sendTemplateSms(String mobiles, String templateId, List
18 //发送短信--start-- 19 String params = null; 20 if(param != null){ 21 params = String(y()); 22 }else{ 23 params = "[]"; 24 } 25 SendRequest submitReq = new SendRequest(); 26 d(apId); 27 ame(ecName); 28 retKey(secretKey); 29 ams(params); 30 iles(mobiles); 31 n(sign); 32 Serial(addSerial); 33 plateId(templateId); 34
35 StringBuffer stringBuffer = new StringBuffer(); 36 (ame()); 37 (d()); 38 (retKey()); 39 (plateId()); 40 (iles()); 41 (ams()); 42 (n()); 43 (Serial()); 44 String encode = ""; 45 try { 46 //接⼝要求参数为MD5加密后的值 47 ((ng())); 48 String reqText = String(submitReq); 49 //base64编码 50 encode =new String(Base64Chunked(es("UTF-8"))); //有中⽂时使⽤UTF-8 51 } catch (UnsupportedEncodingException e1) { 52 tackTrace(); 53 } 54 String resStr = ""; 55 try { 56 resStr = new String(stTrust(templateurl,encode,"utf-8","POST"));//https链接 57 } catch (Exception e) { 58 tackTrace(); 59 } 60 //发送短信--end-- 61
62 //响应状态、回执状态处理--start-- 63 SendResponse sendRes = bject(resStr,);//响应状态 64 String receiptReport=getReport(apId,secretKey,ecName);//回执状态 66 String errorCode=""; 67 if(receiptReport!="[]"){ 68 JSONArray jsonArray= rray(receiptReport); 69 if(ns("errorCode")){ 70 for(int index=0;index<();index++){ 71 JSONObject jsonObject = NObject(index); 72 if(ing("mobile").equals(mobiles)) { 73 errorCode=("errorCode").toString(); 74 } 75 } 76 } 77 } 78 else{ 79 errorCode=""; 80 } 81 if(cess() && !"".equals(Group()) && "success".equals(cod())){ 82 switch (cod()) { 83 case "success": 84 if (errorCode != null && errorCode != "") { 85 return (11, "其他错误:" + errorCode+",请联系管理员!"); 86 } else { 87 return (0, "发送成功"); 88 } 89 case "IllegalMac ": 90 return (1, "mac校验不通过"); 91 case "IllegalSignId ": 92 return (2, "⽆效的签名编码"); 93 case "InvalidMessage ": 94 return (3, "⾮法消息,请求数据解析失败"); 95 case "InvalidUsrOrPwd ": 96 return (4, "⾮法⽤户名/密码"); 97 case "NoSignId ": 98 return (5, "未匹配到对应的签名信息"); 99 case "TooManyMobiles ":100 return (6, "⼿机号数量超限,应≤5000");101 default:102 return (-1, "系统异常");103 }104 }105 else{106 return (-2,"请求异常");107 }108 //响应状态、回执状态处理--end--109 }110
111 //获取状态报告112 public 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 d(apId);117 ame(ecName);118 retKey(secretKey);119
120 StringBuffer stringBuffer = new StringBuffer();121 (ame());122 (d());123 (retKey());124 (plateId());125 String encode = "";126 try {127 //接⼝要求参数为MD5加密后的值128 ((ng()));129 String reqText = String(submitReq);130 //base64编码131 encode =new String(Base64Chunked(es("UTF-8"))); //有中⽂时使⽤UTF-8132 } catch (UnsupportedEncodingException e1) {133 tackTrace();134 }135 String receiptReport = "";136 try {137 receiptReport = new String(stTrust(reporturl,encode,"utf-8","POST"));//https链接138 139 } catch (Exception e) {140 tackTrace();141 }142 return receiptReport;143 }
---->sendPostTrust 1 //添加信任主机 2 private static void trustAllHosts() { 3 // 创建不验证证书链的信任管理器 这⾥使⽤的是x509证书 4 TrustManager[] trustAllCerts = new TrustManager[]{new TrustAnyTrustManager() { 5 public .X509Certificate[] getAcceptedIssuers() { 6 return new .X509Certificate[]{}; 7 } 8
9 public void checkClientTrusted(X509Certificate[] chain, String authType) { 10 } 11
12 public void checkServerTrusted(X509Certificate[] chain, String authType) { 13 } 14 }}; 15 // 安装所有信任的信任管理器 16 try { 17 SSLContext sc = tance("TLS"); 18 (null, trustAllCerts, new Random()); 19 //HttpsURLConnection通过SSLSocket来建⽴与HTTPS的安全连接,SSLSocket对象是由SSLSocketFactory⽣成的。 20 aultSSLSocketFactory(ketFactory()); 21 } catch (Exception e) { 22 tackTrace(); 23 } 24 } 25
26 /** 27 * 发送post 数据并忽略证书校验 28 * @param urls 29 * @return 30 */ 31 public 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; 36 try { 37 trustAllHosts(); 38 // 创建url资源 39 URL url = new URL(urls); 40 // 建⽴http连接 41 HttpsURLConnection conn = (HttpsURLConnection) nnection(); 42 tnameVerifier(DO_NOT_VERIFY); 43 // 设置不⽤缓存 44 Caches(false); 45 // 设置允许输出 46 utput(true); 47 // 设置允许输⼊ 48 nput(true); 49 // 设置传递⽅式 50 uestMethod(method); 51 //n(uestMethod()); 52 // 设置维持长连接 53 uestProperty("Connection", "Keep-Alive"); 54 // 设置⽂件字符集: 55 uestProperty("Charset", "UTF-8"); 56 // 转换为字节数组 57 // byte[] data = (param).getBytes(); 58 // // 设置⽂件长度 59 // uestProperty("Content-Length", f()); 60 // 设置⽂件类型: 61 uestProperty("Content-Type", contentType); 62 // 开始连接请求 63 t(); 64 out = new DataOutputStream(putStream()); 65 // 写⼊请求的字符串 66 ytes(param); 67 (); 68
69 //n(ponseCode()); 70
71 // 请求返回的状态 72 if (_OK == ponseCode()) { 73 n("连接成功"); 74 // 请求返回的数据 75 in1 = utStream(); 76 String readLine; 77 responseReader = new BufferedReader(new InputStreamReader(in1)); 78 while((readLine=ne()) != null){ 79 (readLine).append("n"); 80 } 81 } else { 82 n("error++"); 83 } 84 } catch (Exception e) { 85
86 } finally { 87 try { 88 if (null != responseReader) 89 (); 90 if (null != in1) 91 (); 92 } catch(Exception e) {} 93 try { 94 (); 95 } catch(Exception e) {} 96 } 97
98 return ng(); 99
100 }
发布评论