Java对接云mas发送短信(http方式)

发布时间 2023-03-29 11:35:47作者: 全琪俊

一、官网下载对接文档

http://mas.10086.cn/login

 

 

 

 

二、登录云平台配置账户

管理–>接口管理–>新建短信接口建立自己的用户信息

 

 

 


三、建立好账户后导出证书(用于对接)

 

 

 

 

 

 

 

 

 

 

四、编写Java对接代码

注:网关签名编码既第三步导出签名的编码

1.application.yml

#短信验证码
smscode:
#apid
apId: xxxx
#密码
secretKey: xxxx
#集团名称
ecName: xxxx
#网关签名编码
sign: xxxx
#请求url
url: https://112.35.10.201:28888/sms/submit

 

 

2.SMSUtil.java


import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

@Component
public class SMSUtil {
@Value("${smscode.apId}")
public String apId;
@Value("${smscode.secretKey}")
public String secretKey;
@Value("${smscode.ecName}")
public String ecName;//集团名称
@Value("${smscode.sign}")
public String sign; //网关签名编码
// 移动云mas 的请求url 路径 https 和htpp的不一致 要注意
//https--- https://112.35.10.201:28888/sms/tmpsubmit
//http---http://112.35.1.155:1992/sms/norsubmit
@Value("${smscode.url}")
public String url;

private String addSerial=""; //拓展码 填空



/**
* 多用户发送短信信息
* @param mobiles 手机号码逗号分隔
* @param content 短信内容
* @return 返回1表示成功,0表示失败
* @throws IOException
*/
public int sendMsg(String mobiles,String content) throws IOException {
// Calendar calendar = Calendar.getInstance();
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// String nowDatestr = sdf.format(calendar.getTimeInMillis());
// content += nowDatestr; //短信内容后跟个日期时间(可有可无),需求要求

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());


String aCase = DigestUtil.md5Hex(stringBuffer.toString()).toLowerCase();
sendReq.setMac(aCase);

String reqText = JSONUtil.toJsonStr(sendReq);

String code64= Base64.encode(reqText, "utf-8");



String resStr = HttpUtil.post(url, code64);
//此处使用hutool工具包httpRequest工具类发送https请求
// String resStr = HttpRequest.post(url)
// .header("contentType", "utf-8")
// .body(code64)
// .execute()
// .body();

System.out.println("发送短信结果:" + resStr);

SendRes sendRes = JSON.parseObject(resStr, SendRes.class);

if (sendRes.isSuccess() && !"".equals(sendRes.getMsgGroup()) && "success".equals(sendRes.getRspcod())) {
return 1;
} else {
return 0;
}
}


/**
* 向指定 URL 发送POST方法的请求
*
* @param url
* 发送请求的 URL
* @param param
* 请求参数
* @return 所代表远程资源的响应结果
*/
private static String sendPost(String url, String param) {
OutputStreamWriter out = null;

BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("contentType","utf-8");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
conn.setDoOutput(true);
conn.setDoInput(true);

out = new OutputStreamWriter(conn.getOutputStream());
out.write(param);
out.flush();


in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += "\n" + line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}


}

3.调用短信发送方法


@Resource
private SMSUtil smsUtil;
String msgContent= "";
String mobiles = "";
int sendMsg = smsUtil.sendMsg(mobiles, msgContent);
if(sendMsg>0){
ActivityPublish byId = this.getById(id);
if(ObjectUtil.isNotEmpty(byId)){
Integer smsNum = byId.getSmsNum();
if(ObjectUtil.isNotNull(smsNum)){
smsNumAll=smsNum +1;
}else{
smsNumAll =1;
}

}
return R.failed("消息发送成功!");
}else{
return R.failed("消息发送失败!");
}