记录对接移动Mas平台发送短信踩的坑
1、
2、第一步,在已经注册完申请过云Mas平台账号之后,登录: 移动云Mas平台.
3、登录之后找到 管理 — 接口管理界面,填写接口注册信息,注意每次修改时都会提示用户密码不允许和最近五次相同,所以建议最好还是一次成功,主要用到用户名,密码,对于IP来说,目前没有发现有什么作用
4、注册成功之后会进入短信接入用户管理界面,在这里可以看到已经注册成功的用户,点击签名下载获取到接口所需要的签名编码:
5、到此准备工作就完成了,接下来就是调用环节了,遍地是坑 :
传入类,虽然接口文档中没写要传secretKey,但mac参数中写需要用户密码,如果不仔细看说明很容易忽略
public class SendReq {
private String ecName; //集团客户名称
private String apId; //用户名
private String secretKey; //密码
private String mobiles; //手机号码逗号分隔。(如“18137282928,18137282922,18137282923”)
private String content; //发送短信内容
private String sign; //网关签名编码,必填,签名编码在中国移动集团开通帐号后分配,可以在云MAS网页端管理子系统-SMS接口管理功能中下载。
private String addSerial; //扩展码,根据向移动公司申请的通道填写,如果申请的精确匹配通道,则填写空字符串(""),否则添加移动公司允许的扩展码。
private String mac; //API输入参数签名结果,签名算法:将ecName,apId,secretKey,mobiles,content ,sign,addSerial按照顺序拼接,然后通过md5(32位小写)计算后得出的值。
}
public class SendRes {
private String rspcod; //响应状态码
private String msgGroup; //消息批次号,由云MAS平台生成,用于验证短信提交报告和状态报告的一致性(取值msgGroup)注:如果数据验证不通过msgGroup为空
private boolean success; //数据校验结果
}
移动接口ip地址区分平台 不同平台地址不一样,所以ip地址需要自己找客户去要
账号密码就是刚刚注册的接口用户名和密码,签名在导出的excel中
public class MasUtil {
private static String apId=""; //用户名
private static String secretKey=""; //密码
private static String ecName = ""; //集团名称
private static String sign = ""; //网关签名编码
private static String addSerial = ""; //拓展码 填空
public static String url = "";//请求url
/**
* 多用户发送短信信息
* @param phone 手机号码逗号分隔
* @return 返回1表示成功,0表示失败
* @throws IOException
*/
public static int sendMsg(String phone) throws IOException{
String code = "1111";
String content = "您本次登录的验证码为:"+code+"";
SendReq sendReq = new SendReq();
sendReq.setApId(apId);
sendReq.setEcName(ecName);
sendReq.setSecretKey(secretKey);
sendReq.setContent(content);
sendReq.setMobiles(phone);
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());
//System.out.println(stringBuffer.toString());
sendReq.setMac(MD5Util.MD5(stringBuffer.toString()).toLowerCase());
//System.out.println(sendReq.getMac());
String reqText = JSON.toJSONString(sendReq);
String encode = Base64.getEncoder().encodeToString(reqText.getBytes("UTF-8"));
//System.out.println(encode);
String resStr = "";
try {
resStr = HttpClient.sendPost(url,encode,"");
} catch (Exception e) {
e.printStackTrace();
}
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;
}
//return 1;
}
public static void main(String[] args) throws IOException {
sendMsg("");
}
因为移动接口都是https形式,所以平常的Http方式的post请求可能会出现:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building f报错,需要处理跳过证书认证,所以直接下附工具类
public class HttpClient {
//这里用到了内部类
private static class TrustAnyTrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
private static class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
public static String sendPost(String url, String param,String token) throws Exception {
//PrintWriter out = null;
//需要用outputStreamWriter
//新增SSL安全信任
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
//end
OutputStreamWriter out=null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
//打开和URL之间的连接
HttpsURLConnection conn = (HttpsURLConnection)realUrl.openConnection();
//新增conn连接属性
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
//end
//设置通用的请求属性
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", token);
//发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
//获取URLConnection对象对应的输出流
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
//out = new PrintWriter(conn.getOutputStream());
//发送请求参数
out.append(param);
//out.print(param);
//flush输出流的缓冲
out.flush();
//定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(),"UTF-8"));
String line;
while ((line = in .readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送POST请求出现异常!" + e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if ( in != null) {
in .close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}
虽然解决方案不太麻烦,但如果第一次对接的话可能会手忙脚乱,找不到ip地址,用错登录名或签名,特此将整个流程记录,以做备份