Java对接云mas发送短信(https方式)
最近做业务需要调用短信服务,参照网上的demo自己编写了个调用工具类。
https://blog.csdn.net/xyf13920745534/article/details/121624490
实现代码
实体类
短信业务DTO SendReq.java
/**
* 短信业务DTO
*
* @author xxxxx
* @date 2022/5/26
*/
public class SendReq {
/**集团客户名称*/
private String ecName;
/**用户名*/
private String apId;
/**密码*/
private String secretKey;
/**手机号码逗号分隔。(如“1813728****,1813728****,1813728****”)*/
private String mobiles;
/**发送短信内容*/
private String content;
/**网关签名编码,必填,签名编码在中国移动集团开通帐号后分配,可以在云MAS网页端管理子系统-SMS接口管理功能中下载。*/
private String sign;
/**扩展码,根据向移动公司申请的通道填写,如果申请的精确匹配通道,则填写空字符串(""),否则添加移动公司允许的扩展码。*/
private String addSerial;
/**按文档要求加密后的mac*/
private String mac;
/*** getter&setter ***/
}
MAS短信服务响应VO SendRes.java
/**
* MAS短信服务响应VO
*
* @author xxxxx
* @date 2022/5/26
*/
public class SendRes {
/**响应状态码*/
private String rspcod;
/**消息批次号,由云MAS平台生成,用于验证短信提交报告和状态报告的一致性(取值msgGroup)注:如果数据验证不通过msgGrou*/
private String msgGroup;
/**数据校验结果*/
private boolean success;
/*** getter&setter ***/
}
调用工具类
SMSUtils.java
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.sign.Md5Utils;
import com.xxxxx.pipeline.model.SendReq;
import com.xxxxx.pipeline.model.SendRes;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
/**
* 云MAS平台短信服务工具类
*
* @author xxxxx
* @date 2022/5/26
*/
public class SMSUtils {
// 账号
private static final String apId = "xxxxxx";
// 密码
private static final String secretKey = "xxxxxx";
// 集团名称
private static final String ecName = "xxxxxx";
// 网关签名编码
private static final String sign = "xxxxxx";
// 拓展码(可以为空)
private static final String addSerial = "";
// url
private static final String URL = "https://112.35.10.201:28888/sms/submit";
// 日志打印
private static final Logger logger = LoggerFactory.getLogger(SMSUtils.class);
/**
* 多用户发送短信信息
*
* @param mobiles 手机号码逗号分隔
* @param content 短信内容
* @return 返回1表示成功,0表示失败
* @throws IOException
*/
public static boolean sendMsg(String mobiles, String content) throws IOException {
// 设置请求参数
SendReq sendReq = new SendReq();
sendReq.setApId(apId);
sendReq.setEcName(ecName);
sendReq.setSecretKey(secretKey);
// 发送短信内容
sendReq.setContent(content);
// 手机号码逗号分隔
sendReq.setMobiles(mobiles);
sendReq.setAddSerial(addSerial);
sendReq.setSign(sign);
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(sendReq.getEcName());
stringBuffer.append(sendReq.getApId());
stringBuffer.append(sendReq.getSecretKey());
stringBuffer.append(sendReq.getMobiles());
stringBuffer.append(sendReq.getContent());
stringBuffer.append(sendReq.getSign());
stringBuffer.append(sendReq.getAddSerial());
// 加密处理
sendReq.setMac(Md5Utils.hash(stringBuffer.toString()).toLowerCase());
String reqText = JSON.toJSONString(sendReq);
// BASE64编码
String encode = Base64.encodeBase64String(reqText.getBytes("UTF-8"));
System.out.println(encode);
//此处使用hutool工具包httpRequest工具类发送https请求
String resStr = HttpRequest.post(URL)
.header("contentType", "utf-8")
.body(encode)
.setConnectionTimeout(15000)
.setReadTimeout(15000)
.execute()
.body();
logger.info("发送短信结果:" + resStr);
SendRes sendRes = JSON.parseObject(resStr, SendRes.class);
if (sendRes.isSuccess() && !"".equals(sendRes.getMsgGroup()) && "success".equals(sendRes.getRspcod())) {
return true;
} else {
return false;
}
}
/**
* main方法测试发送短信
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String msg = "这是发送短信的内容!";
sendMsg("1314******", msg);
}
pom文件
MAVEN引入的Jar包比较多,这里简单列举几个必须的。
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.22</version>
</dependency>
写在最后
在测试时短信服务账号和密码时,用了一段Python代码,也可以发送短信,这里也进行下记录,参考自
服务调用 https://blog.csdn.net/u013399150/article/details/116487110
import urllib3, certifi
import hashlib
import base64, json
'''
短信服务调用 https://blog.csdn.net/u013399150/article/details/116487110
'''
content_a = {
"ecName": "XXXXXX",
"apId": "XXXXXX",
"mobiles": "XXXXXX",# 手机号码逗号分隔。(如“1813728****,1813728****,1813728****”)
"content": "XXXXXX。",
"sign": "XXXXXX",
"addSerial": "",
"mac": ""
}
secret_key = 'Hngxb@123'
g_mac = hashlib.md5()
g_mac_cont = content_a['ecName'] + content_a['apId'] + secret_key + content_a['mobiles'] + content_a['content'] + content_a['sign'] + content_a['addSerial']
g_mac.update(g_mac_cont.encode('utf-8'))
content_a["mac"] = (g_mac.hexdigest()).lower()
http_pool = urllib3.PoolManager(cert_reqs='CERT_NONE')
encoding_content = base64.b64encode(json.dumps(content_a).encode('utf-8'))
response = http_pool.request("POST", "https://112.35.10.201:28888/sms/submit",
body=encoding_content, headers={'Content-Type': 'application/json'})
print(response.status)
print(response.data)