发送短信工具类SMSClient.java

package com.dhxx.common.mas;import com.dhxx.common.util.JSONUtils;
import com.dhxx.common.util.Md5Util;
import org.apache.commons.codec.binary.Base64;import java.io.*;
import java.net.*;public class SMSClient {private static String apId="xxxxxx";private static String secretKey="xxxxxx";private static String ecName = "xxxxxx";//集团名称private static String sign = "70Ldxxxxx";//网关签名编码private static String addSerial = "";//拓展码 填空public static String msg = "这是发送短信的内容!";public static String url = "http://112.35.1.155:1992/sms/norsubmit";//请求urlpublic static int sendMsg(String mobiles,String content){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());//System.out.println(stringBuffer.toString());sendReq.setMac(Md5Util.MD5(stringBuffer.toString()).toLowerCase());//System.out.println(sendReq.getMac());String reqText = JSONUtils.obj2json(sendReq);String encode = Base64.encodeBase64String(reqText.getBytes());//System.out.println(encode);String resStr = sendPost(url,encode);System.out.print("发送短信结果:"+resStr);SendRes sendRes = JSONUtils.json2pojo(resStr,SendRes.class);if(sendRes.isSuccess() && !"".equals(sendRes.getMsgGroup()) && "success".equals(sendRes.getRspcod())){return 1;}else{return 0;}}/*** main方法测试发送短信,返回1表示成功,0表示失败*/public static void main(String[] args){int result = sendMsg("xxxxxx",msg);System.out.println(result);}/*** 向指定 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;}}

这里需要注意的是apId和secretKey一般不会搞错,但是集团名称ecName一定要写对,否则就会提示{“msgGroup”:”“,”rspcod”:”InvalidUsrOrPwd”,”success”:false},无效的用户名和密码,还有一个就是mac字段sendReq.setMac(Md5Util.MD5(stringBuffer.toString()).toLowerCase());
MD5加密过后一定要把它变成小写.toLowerCase(),否则也会出错。

发送短信实体SendReq.java

package com.dhxx.common.mas;/*** 发送短信请求实体*/
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 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;}
}

响应发送结果实体SendRes.java

package com.dhxx.common.mas;/*** 发送短信响应实体*/
public class SendRes {private String rspcod;//响应码private String msgGroup;//消息批次号,由云MAS平台生成,用于验证短信提交报告和状态报告的一致性(取值msgGroup)注:如果数据验证不通过msgGroup为空private boolean success;public String getRspcod() {return rspcod;}public void setRspcod(String rspcod) {this.rspcod = rspcod;}public String getMsgGroup() {return msgGroup;}public void setMsgGroup(String msgGroup) {this.msgGroup = msgGroup;}public boolean isSuccess() {return success;}public void setSuccess(boolean success) {this.success = success;}
}

鉴于有朋友要求看json类和MD5工具类,这里也贴出来
MD5工具类Md5Util.java:

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;}}/*** 先进行HmacSHA1转码再进行Base64编码* @param data  要SHA1的串* @param key   秘钥* @return* @throws Exception*/public static String HmacSHA1ToBase64(String data, String key) throws Exception {SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");  Mac mac = Mac.getInstance("HmacSHA1");  mac.init(signingKey);  byte[] rawHmac = mac.doFinal(data.getBytes());  return Base64.encodeBase64String(rawHmac);}/*** 校验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());}
}

json工具类JSONUtils.java:

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;/*** @author jhy* @description  json工具*/
public class JSONUtils {private final static ObjectMapper objectMapper = new ObjectMapper();private static Logger log = LoggerFactory.getLogger(JSONUtils.class);private JSONUtils() {}public static ObjectMapper getInstance() {return objectMapper;}/*** javaBean,list,array convert to json string*/public static String obj2json(Object obj) {try {return objectMapper.writeValueAsString(obj);} catch (JsonProcessingException e) {// TODO Auto-generated catch blocklog.error(e.getMessage());}return null;}/*** javaBean,list,array convert to json string*/public static String obj2jsonInoreString(Object obj)  {try {return objectMapper.writeValueAsString(obj);} catch (JsonProcessingException e) {// TODO Auto-generated catch blocklog.error(e.getMessage());}return null;}/*** json string convert to javaBean*/public static <T> T json2pojo(String jsonStr, Class<T> clazz) {try {return objectMapper.readValue(jsonStr, clazz);} catch (JsonParseException e) {// TODO Auto-generated catch blocklog.error(e.getMessage());} catch (JsonMappingException e) {// TODO Auto-generated catch blocklog.error(e.getMessage());} catch (IOException e) {// TODO Auto-generated catch blocklog.error(e.getMessage());}return null;}/*** json string convert to map*/public static <T> Map<String, Object> json2map(String jsonStr) {try {return objectMapper.readValue(jsonStr, Map.class);} catch (JsonParseException e) {// TODO Auto-generated catch blocklog.error(e.getMessage());} catch (JsonMappingException e) {// TODO Auto-generated catch blocklog.error(e.getMessage());} catch (IOException e) {// TODO Auto-generated catch blocklog.error(e.getMessage());}return null;}/*** json string convert to map with javaBean*/public static <T> Map<String, T> json2map(String jsonStr, Class<T> clazz){Map<String, Map<String, Object>> map = null;try {map = objectMapper.readValue(jsonStr,new TypeReference<Map<String, T>>() {});} catch (JsonParseException e) {// TODO Auto-generated catch blocklog.error(e.getMessage());} catch (JsonMappingException e) {// TODO Auto-generated catch blocklog.error(e.getMessage());} catch (IOException e) {// TODO Auto-generated catch blocklog.error(e.getMessage());}Map<String, T> result = new HashMap<String, T>();for (Map.Entry<String, Map<String, Object>> entry : map.entrySet()) {result.put(entry.getKey(), map2pojo(entry.getValue(), clazz));}return result;}/*** json array string convert to list with javaBean*/public static <T> List<T> json2list(String jsonArrayStr, Class<T> clazz){List<Map<String, Object>> list = null;try {list = objectMapper.readValue(jsonArrayStr,new TypeReference<List<T>>() {});} catch (JsonParseException e) {// TODO Auto-generated catch blocklog.error(e.getMessage());} catch (JsonMappingException e) {// TODO Auto-generated catch blocklog.error(e.getMessage());} catch (IOException e) {// TODO Auto-generated catch blocklog.error(e.getMessage());}List<T> result = new ArrayList<T>();for (Map<String, Object> map : list) {result.add(map2pojo(map, clazz));}return result;}/*** map convert to javaBean*/public static <T> T map2pojo(Map map, Class<T> clazz) {return objectMapper.convertValue(map, clazz);}
}

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

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

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

  2. VS2019 c# 中国移动云mas平台 webservice实现

    云MAS提供webservice服务,接收客户端向云MAS平台发送请求,带上相应的请求参数,云MAS平台接收请求,并进行验证,验证通过后进行短信发送. 本文实现一对多短信发送 接口文档:中国移动云ma ...

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

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

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

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

  5. SpringBoot集成移动云MAS平台(SDK版本)

    公司的一个街道项目的微信公众号需要发短信的验证码,需求原型如下图,对比了三大运营商,移动的云MAS平台,不需要硬件,直接联系移动客户经理注册即可,可以通过SDK,WebService,CMPP协议等方 ...

  6. 云运维管家服务器,行云管家云管平台私有部署标准版安装与体验

    行云管家云管平台 作为业界领先的多云管理平台,行云管家提供针对多家云厂商.多种云资源的一站式管理解决方案,帮助客户:易上云.用好云.管好云 行云管家内置堡垒机模块,从功能上来说,它是传统堡垒机的功能超 ...

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

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

  8. 通达OA短信平台,通达OA与天瑞短信平台深度集成,安全可靠,方便快捷

    通达OA与天瑞短信平台深度集成 1.通达OA网络办公系统   http://www.tongda2000.com 2.天瑞短信平台 http://www.wasun.cn/ 短信平台登录地址:http ...

  9. 【微信小程序 - 工作实战分享】1.微信小程序发送手机短信验证码(阿里云)

    发送手机短信验证码 前言 一. 准备工作 二. 配置 三. 实战代码(仅仅是后台代码,前端传入手机号) 总结 前言 在网站和移动应用中利用短信验证码进行信息确认是最常用的验证手段.随着短信验证码的技术 ...

最新文章

  1. oracle中的sql%rowcount
  2. 如何在Elasticsearch中进行深分页
  3. LeetCode 1237. 找出给定方程的正整数解
  4. java 文件下载方法_【工具类】Java后台上传下载文件的几种方式
  5. 自学python编程笔记本推荐-python自学教程 | 3万字为你详解每个重要知识点
  6. jquery on()方法off()方法
  7. Zemax操作24--高斯光束的聚焦和传播
  8. Access2016学习8
  9. DNS是什么?工作原理、工作流程总结
  10. C# 切割超级大图(.bmp)[1G以上超大图片分块加载代码]
  11. 多人共享的待办事项app有哪些
  12. 关系模式设计优化(数据库学习重点,难点)
  13. 【量化策略】横盘策略20211209
  14. R语言回归分析-回归诊断
  15. Taro-RN使用 react-native-wechat-lib 集成微信支付-IOS(从微信注册应用到应用接入微信支付)全*
  16. 《游戏改变世界》改变了对游戏的认知
  17. 精英任务 | 第二期券商研报复现挑战赛
  18. Win11快捷方式使用IE浏览器(非其它浏览器的IE模式)
  19. 最新勒索病毒分类完整合集,勒索病毒后缀邮箱集合(统计实时更新……)
  20. 机器视觉——相机曝光与帧率的关系

热门文章

  1. mac中clion无法运行
  2. BZOJ 1507 Editor
  3. shell中expr的使用介绍
  4. python滑块验证(打码)+pillow裁剪图片
  5. 区块链baas平台告警方案
  6. python对excel筛选提取文本中数字_详解利用python提取pdf文本数字
  7. Python学习_2015年12月14日
  8. 3、以太坊智能合约开发(语法开发学习)
  9. iOS-内购注意 沙盒二次验证
  10. 学习笔记二:关于自激振荡