最近做业务需要调用短信服务,参照网上的demo自己编写了个调用工具类。
实现代码 实体类https://blog.csdn.net/xyf13920745534/article/details/121624490
短信业务DTO SendReq.java
public class SendReq {
private String ecName;
private String apId;
private String secretKey;
private String mobiles;
private String content;
private String sign;
private String addSerial;
private String mac;
}
MAS短信服务响应VO SendRes.java
public class SendRes {
private String rspcod;
private String msgGroup;
private boolean success;
}
调用工具类
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;
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);
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;
}
}
public static void main(String[] args) throws IOException {
String msg = "这是发送短信的内容!";
sendMsg("1314******", msg);
}
pom文件
MAVEN引入的Jar包比较多,这里简单列举几个必须的。
cn.hutool
hutool-all
5.7.22
写在最后
在测试时短信服务账号和密码时,用了一段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)



