//短信发送测试

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;
}
}

转载于:https://www.cnblogs.com/hnzkljq/p/10194909.html

移动云Mas发送普通短信和模板短信相关推荐

  1. 移动云mas发送短信一直返回InvalidUsrOrPwd

    移动云mas发送短信一直返回InvalidUsrOrPwd 通过main方法调用sendMsg发送短信成功,可是通过接口调用sendMsg方法时一直返回{"msgGroup":&q ...

  2. 移动云mas 通过HTTP请求发送普通短信和 模板短信

    一.短信发送配置 tnar:mobileCloud:sms:url: http://112.35.1.155:1992/sms/norsubmitecName: 公司名称apId: 账号secretK ...

  3. Java对接云mas发送短信(https方式)

    一.官网下载对接文档 http://mas.10086.cn/login 二.登录云平台配置账户 管理–>接口管理–>新建短信接口建立自己的用户信息 三.建立好账户后导出证书(用于对接) ...

  4. 移动MAS发送短信,接受状态和接受回复【HTTP】

    移动MAS短信平台就是一个发送短信的平台,可以发送普通短信.模板短信.一对一,一对多等短信 下面主要是基于Java(HTTP方式)实现短信的下行(发送)上行(接受)和接受短信状态 接口全是根据HTTP ...

  5. 中国移动云MAS平台发送普通短信

    使用中国移动云MAS平台发送普通短信 步骤 1.输入用户名和密码登录中国移动云MAS业务平台. 下载用户操作手册 和 HTTP接口文档 深入了解云MAS 2.在中国移动云MAS业务平台的主页面,点击[ ...

  6. 中国移动 云MAS平台HTTP2.1(HTTP版)发送普通短信

    发送短信工具类SMSClient.java package com.dhxx.common.mas;import com.dhxx.common.util.JSONUtils; import com. ...

  7. Java+Demo对接中国移动 云MAS短信发送(http协议详解,新测成功!)

    一.登录官网,下载http接入文档(随着官网不断更新,可参考官网的文档) 官网地址为:云mas业务平台 进入云MAS管理平台,找到 管理-接口管理 的列表页. (必读:本文对接方式是 java引用ja ...

  8. 中国移动云mas短信对接(webservice)

    额,我也不知道弄撒类,移动短信和我这么有缘,对接完http的后来又说因为种种原因不能用,又要重新对接过webservice版本的,没办法,谁叫咱是打工人呢,话不多说,直接开整 一.登录官网,下载web ...

  9. 中国移动云mas短信对接(http)

    一.登录官网,下载http接入文档 官网地址为:云mas业务平台 二.创建http短信接口 登录中国移动云mas平台,新建短信接口: 新建短信接口(简称SMS接口),是为集团客户创建可以使用接口发送短 ...

最新文章

  1. 关于MonoBehaviour的单例通用规则
  2. python3迭代器和可迭代对象_一文读懂 Python3 可迭代对象、迭代器、生成器区别...
  3. S11 Linux系统管理命令
  4. 内核中断处理流程_处理中断
  5. 网站关停就没事了?5100万账户文件被盗
  6. Docker:单机编排工具docker-compose [十二]
  7. 让开发人员变平庸的八个习惯,看看你中了几条
  8. iis7.5 php虚拟站点目录设置,windows2008中IIS7.5环境下 Fastcgi模式PHP配置教程
  9. 常见的锁策略、synchronized中的锁优化机制
  10. codevs 1164 统计数字
  11. linux服务器的性能分析与优化(十三)
  12. 天王表的网络营销战略
  13. shell:sed 替换换行符
  14. SpringMVC静态资源配置
  15. PPT设置自动保存时间 mac_如何一键把Word转换为PPT?
  16. Linda Rising:“你相信谁?”
  17. QCA三天写论文!模糊集数据校准实战
  18. 微信开发者工具如何模拟调试扫描小程序二维码功能
  19. 品牌商业模式调研竞品市场分析方案模板ppt
  20. Android的log机制,androidtv开发总结

热门文章

  1. ZYNQ-使用HDMI显示器进行SD卡图片读取显示
  2. 谷歌浏览器弹出窗口html代码,谷歌浏览器显示通知消息JS代码
  3. 解决谷歌浏览器跨域问题
  4. tensorflow实现对图片的读取(tf.image.decode_jepg和tf.image.decode_png)
  5. 前端工程中常用的文件夹命名(扫盲帖)
  6. 一楼到十楼的每层电梯门口都放着一颗钻石,钻石大小不一。你乘坐电梯从一楼到十楼,每层楼电梯门都会打开一次,只能拿一次钻石,问怎样才能拿到最大的一颗?
  7. 深入探究802.11ac技术
  8. 前端基础知识学习总结--百分比布局、Flex布局
  9. python xlrdxlwt应用 以文本形式存储数字 数字前补零
  10. Android AsyncTask的使用及泛式参数