移动云Mas发送普通短信和模板短信

Posted hnzkljq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了移动云Mas发送普通短信和模板短信相关的知识,希望对你有一定的参考价值。

//短信发送测试

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;
import com.huazi.projects.entity.SubmitReq;
public class SmsSend {
private static String apId="*****";用户名
private static String secretKey="*****";用户密码
private static String ecName = "*****";//集团名称(分组)
private static String sign = "*****";//签名编码
private static String addSerial = "";//拓展码
public static String url = "http://112.35.1.155:1992/sms/tmpsubmit";//请求url 该接口为模板短信 //http://112.35.1.155:1992/sms/norsubmit  //普通短信接口public static void main(String[] args) {

//String[] param = {"*****","****"};//模板相关的变量参数
String mobiles = "*******";//手机号,多个手机号用英文的 , 隔开

String templateId = "*****"," ;   //模板ID
SmsSend.smsSend(templateId , param, mobiles);
}

 

//模板信息
public static String smsSend(String templateId, String[] param,String mobiles) {
String params = null;
if(param != null){
params = JsonUtil.object2json(param);
}else{
params = "[]";
}
SubmitReq submitReq = new SubmitReq();
submitReq.setApId(apId);
submitReq.setEcName(ecName);
submitReq.setSecretKey(secretKey);
submitReq.setParams(params);
submitReq.setMobiles(mobiles);
submitReq.setSign(sign);
submitReq.setAddSerial(addSerial);
submitReq.setTemplateId(templateId);
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(submitReq.getEcName());
stringBuffer.append(submitReq.getApId());
stringBuffer.append(submitReq.getSecretKey());
stringBuffer.append(submitReq.getTemplateId());
stringBuffer.append(submitReq.getMobiles());
stringBuffer.append(submitReq.getParams());
stringBuffer.append(submitReq.getSign());
stringBuffer.append(submitReq.getAddSerial());
String encode = "";
try {

//接口要求参数为MD5加密后的值
submitReq.setMac(Md5Util.MD5(stringBuffer.toString()));
String reqText = JsonUtil.object2json(submitReq);

//
encode =new String(Base64.encodeBase64Chunked(reqText.getBytes("UTF-8")));  //有中文时使用UTF-8 
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String resStr = "";
try {
resStr = new String(HttpUtil.post(HttpUtil.ContentType_JSON_UTF8,url,encode.getBytes()));//http链接
} catch (Exception e) {
e.printStackTrace();
}
return resStr;
}

//普通信息
public static String norsubmit(String url ,String content ,String mobiles){
SubmitReq submitReq = new SubmitReq();
submitReq.setApId(apId);
submitReq.setEcName(ecName);
submitReq.setSecretKey(secretKey);
submitReq.setContent(content);//短信内容
submitReq.setMobiles(mobiles);
submitReq.setSign(sign);
submitReq.setAddSerial(addSerial);
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(submitReq.getEcName());
stringBuffer.append(submitReq.getApId());
stringBuffer.append(submitReq.getSecretKey());
stringBuffer.append(submitReq.getMobiles());
stringBuffer.append(submitReq.getContent());
stringBuffer.append(submitReq.getSign());
stringBuffer.append(submitReq.getAddSerial());
String encode = "";
String resStr = "";
try {
submitReq.setMac(Md5Util.MD5(stringBuffer.toString()));
String reqText = JsonUtil.object2json(submitReq);
encode =new String(Base64.encodeBase64Chunked(reqText.getBytes("UTF-8")));
resStr = new String(HttpUtil.post(HttpUtil.ContentType_JSON_UTF8,url,encode.getBytes()));
} catch (Exception e) {
e.printStackTrace();
}
return resStr;
}
}

 

 

 

//httpUtil工具类

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;

public class HttpUtil {

public static final String ContentType_BIN = "application/octet-stream";
public static final String ContentType_FORM = "application/x-www-form-urlencoded";
public static final String ContentType_XML_UTF8 = "text/plaint; charset=utf-8";
public static final String ContentType_JSON_UTF8 = "application/json; charset=utf-8";
public static final String ContentType_html = "text/html";
public static final String ContentType_TXT = "text/plain";

public static byte[] post(String contentType, String url, byte[] content) throws IOException {
return doRequest(contentType, "POST", url, content, null);
}

public static byte[] get(String contentType, String url) throws IOException {
return doRequest(contentType, "GET", url, null, null);
}

public static byte[] doRequest(String contentType, String method, String url, byte[] content, Map<String, String> headers) throws IOException {
URL u = null;
HttpURLConnection conn = null;

//尝试发送请求
try {
u = new URL(url);
conn = (HttpURLConnection) u.openConnection();
conn.setRequestMethod(method);
conn.setRequestProperty("Content-Type", contentType);
if (headers != null && ! headers.isEmpty()) {
for(Entry<String, String> item: headers.entrySet()){
conn.setRequestProperty(item.getKey(), item.getValue());
}
}

conn.setConnectTimeout(60000); //1 min
conn.setReadTimeout(600000); //10 min
conn.setUseCaches(false);
if (content != null && content.length >0) {
conn.setDoOutput(true);
java.io.OutputStream paramOut = conn.getOutputStream();
paramOut.write(content);
paramOut.flush();
paramOut.close();
}

ByteArrayOutputStream out = new ByteArrayOutputStream(102400);
InputStream buffIn = new java.io.BufferedInputStream(conn.getInputStream());
int buffsize = 8192;
int bytesRead = 0;
byte[] buffer = new byte[buffsize];
while ((bytesRead = buffIn.read(buffer, 0, buffsize)) != -1)
out.write(buffer, 0, bytesRead);
out.flush();
buffer = null;
return out.toByteArray();
} finally {
if (conn != null) {
conn.disconnect();
}
}
}

static {
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
}

public static void setBasicAuth(String username, String passwd){
Authenticator.setDefault(new BasicAuthenticator(username, passwd));
}

private static class BasicAuthenticator extends Authenticator {
String userName;
String password;

public BasicAuthenticator(String userName, String password) {
this.userName = userName;
this.password = password;
}

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password.toCharArray());
}
}

}

 

 

//MD5Util工具类

import java.security.MessageDigest;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

/**
* MD5加密/验证工具类
*
* @author bluesky
*
*/
public class Md5Util {
static final char hexDigits[] = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘,
‘8‘, ‘9‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘ };

/**
* 生成MD5码
*
* @param plainText
* 要加密的字符串
* @return md5值
*/
public final static String MD5(String plainText) {
try {
byte[] strTemp = plainText.getBytes("UTF-8");
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
return null;
}
}

/**
* 生成MD5码
*
* @param plainText
* 要加密的字符串
* @return md5值
*/
public final static String MD5(byte[] plainText) {
try {
byte[] strTemp = plainText;
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
return null;
}
}


/**
* 校验MD5码
*
* @param text
* 要校验的字符串
* @param md5
* md5值
* @return 校验结果
*/
public static boolean valid(String text, String md5) {
return md5.equals(MD5(text)) || md5.equals(MD5(text).toUpperCase());
}

/**
*
* @param params
* @return
*/
public static String MD5(String... params) {
StringBuilder sb = new StringBuilder();
for (String param : params) {
sb.append(param);
}
return MD5(sb.toString());
}
}

 

 

//实体类

public class SubmitReq {
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 templateId;//模板ID
private String mac;//API输入参数签名结果,签名算法:将ecName,apId,secretKey,mobiles,content ,sign,addSerial按照顺序拼接,然后通过md5(32位小写)计算后得出的值。
private String params;

public String getParams() {
return params;
}

public void setParams(String params) {
this.params = params;
}

public String getTemplateId() {
return templateId;
}

public void setTemplateId(String templateId) {
this.templateId = templateId;
}

public String getEcName() {
return ecName;
}

public void setEcName(String ecName) {
this.ecName = ecName;
}

public String getApId() {
return apId;
}

public void setApId(String apId) {
this.apId = apId;
}

public String getSecretKey() {
return secretKey;
}

public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}

public String getMobiles() {
return mobiles;
}

public void setMobiles(String mobiles) {
this.mobiles = mobiles;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public String getSign() {
return sign;
}

public void setSign(String sign) {
this.sign = sign;
}

public String getAddSerial() {
return addSerial;
}

public void setAddSerial(String addSerial) {
this.addSerial = addSerial;
}

public String getMac() {
return mac;
}

public void setMac(String mac) {
this.mac = mac;
}
}

 










以上是关于移动云Mas发送普通短信和模板短信的主要内容,如果未能解决你的问题,请参考以下文章

C# 调用移动MAS接口发送普通短信WebClient方式

移动MAS短信平台发送短信

Java对接云MAS发送短信验证码(HTTPS方式)

python 调用接口发短信

Springboot 整合 中国移动MAS HTTP1.0 实现短信发送服务

C# 使用阿里云发送短信