亿美软通短信
最近公司在用亿美软通短信,这个整体下来感觉不错。文档写的也很清晰,整合起来也不费事。下面来介绍一下整合步骤;
一
首先找到相关的网站,需要向管理人员获取相关的账号(appId)和秘钥(secretkey16位)
然后下面是
国内接口http标准协议文档和demo的地址:
http://www.b2m.cn/static/doc/sms/onlysms_or.html
接口的url地址:
http://bjmtn.b2m.cn(北京地址)
http://shmtn.b2m.cn (上海地址)
http://gzmtn.b2m.cn (广州地址)
二
读懂相关文档,整合相关代码

讯享网
这里可以查看相关的接口文档,在工作需求中大多会用到单发短信和批量发送。
这个官网上也附带有相关的短信 demo ,有C有Java有PHP,这里我们只看JAVA。
三
整合接口,根据demo进行整理

这些都是软通的工具类 其中有各种加密的方式等等的封装,其中
这些是调用各种短信的例子demo,调用时可以查看。

四
整合项目,实现短信发送,首先在整合时需要查看一些注意事项
必要参数appId,secretKey需向亿美软通获取 默认UTF-8字符集,默认不进行GZip压缩 获取上行和扩展码需账号支持 支持短信单发,群发,获取账号短信剩余条数,获取状态报告,上行 单发短信支持自定义信息id,与扩展码,群发支持扩展码 项目是对亿美软通demo进行简单封装方便使用 PS,短信内容必须带上类似此【前缀】,否则无法发送成功(运营商要求) PS,短信内容含有群发等非法关键字也无法正常发送 本包依赖一个gson-2.6.2.jar
讯享网

MAVEN项目时 ,需引用
讯享网 <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.4</version> </dependency>
接下来只需要写一个工具类就可以了,
这些是demo里可以取出来需要取出来。下面是简单的需要的工具类的例子
@Component public class EmaySMSUtils {
// appId private static String appId = "EUCP-EMY-SMS0-27TIT ";//需要向软通获取 // 密钥 private static String secretKey = "LB663MSJC8W51LET";// 需要向软通获取 // 接口地址 private static String host = "http://bjmtn.b2m.cn";// 北京接口 // 加密算法 private static String algorithm = "AES/ECB/PKCS5Padding"; // 编码 private static String encode = "UTF-8"; // 是否压缩 private static Boolean isGzip = true; / * 发送单条短信 这里R是我所用的架构 封装的 需要什么可以更改 */ public static R sendSingleSms(String content, String mobile){
Boolean flag =checkKeyParam(content,mobile); SysMail sysMail = new SysMail(); if(flag){
content ="【测试】"+content; try {
SmsResponse smsResponse = setSingleSms(appId, secretKey, host, algorithm, content, null, null, mobile, isGzip, encode); return R.ok("发送成功!"); }catch (Exception exception){
return R.fail("发送失败!"); } }else{
return R.fail("发送失败!"); } } / * @描述 群发短信不需要扩展码 * @参数 @param content * @参数 @param mobiles * @参数 @return * @return SmsResponse[] */ public static R sendBatchOnlySms(String content, String[] mobiles){
Boolean flag =checkKeyParam(appId,secretKey,mobiles); SysMail sysMail = new SysMail(); if(flag){
try {
content ="【测试】"+content; //调用批量发送 SmsResponse[] smsResponses = setBatchOnlySms(appId, secretKey, host, algorithm, content, null, mobiles, isGzip, encode); return R.ok("发送成功!"); }catch (Exception exception){
return R.fail("发送失败!"); } }else{
return R.fail("发送失败!"); } } / * 发送单条短信 * * @param isGzip * 是否压缩 * @param content * 内容 */ private static SmsResponse setSingleSms(String appId, String secretKey, String host, String algorithm, String content, String customSmsId, String extendCode, String mobile, boolean isGzip, String encode) {
Boolean flag = checkKeyParam(appId, secretKey, mobile); if (flag) {
System.out.println("=============begin setSingleSms=================="); SmsSingleRequest pamars = new SmsSingleRequest(); pamars.setContent(content); pamars.setCustomSmsId(customSmsId); pamars.setExtendedCode(extendCode); pamars.setMobile(mobile); ResultModel result = request(appId, secretKey, algorithm, pamars, host + "/inter/sendSingleSMS", isGzip, encode); System.out.println("result code :" + result.getCode()); if ("SUCCESS".equals(result.getCode())) {
SmsResponse response = JsonHelper.fromJson(SmsResponse.class, result.getResult()); if (response != null) {
System.out.println("data : " + response.getMobile() + "," + response.getSmsId() + "," + response.getCustomSmsId()); } return response; } System.out.println("=============end setSingleSms=================="); return null; } else {
System.out.println("=============param check error=================="); return null; } } / * 发送批次短信 * * @param isGzip * 是否压缩 */ private static SmsResponse[] setBatchOnlySms(String appId, String secretKey, String host, String algorithm, String content, String extendCode, String[] mobiles, boolean isGzip, String encode) {
Boolean flag = checkKeyParam(appId, secretKey, mobiles); if (flag) {
System.out.println("=============begin setBatchOnlySms=================="); SmsBatchOnlyRequest pamars = new SmsBatchOnlyRequest(); pamars.setMobiles(mobiles); pamars.setExtendedCode(extendCode); pamars.setContent(content); ResultModel result = request(appId, secretKey, algorithm, pamars, host + "/inter/sendBatchOnlySMS", isGzip, encode); System.out.println("result code :" + result.getCode()); if ("SUCCESS".equals(result.getCode())) {
SmsResponse[] response = JsonHelper.fromJson(SmsResponse[].class, result.getResult()); if (response != null) {
for (SmsResponse d : response) {
System.out.println("data:" + d.getMobile() + "," + d.getSmsId() + "," + d.getCustomSmsId()); } } return response; } System.out.println("=============end setBatchOnlySms=================="); return null; } else {
System.out.println("=============param check error=================="); return null; } } @PostConstruct // 初始化 public void init(){
emaySMSUtils = this; emaySMSUtils.iSysMailService = this.iSysMailService; } / * * @描述 校验key与secret * @参数 @param appId * @参数 @param secretKey * @参数 @param mobile * @参数 @return * @return Boolean */ private static Boolean checkKeyParam(String appId, String secretKey) {
if (null == appId || secretKey == null || appId.trim().equals("") || secretKey.trim().equals("")) {
return false; } return true; } / * * @描述 校验key与secret,mobile * @参数 @param appId * @参数 @param secretKey * @参数 @param mobile * @参数 @return * @return Boolean */ private static Boolean checkKeyParam(String appId, String secretKey, String mobile) {
if (null == appId || secretKey == null || mobile == null || appId.trim().equals("") || secretKey.trim().equals("") || mobile.trim().equals("") || mobile.trim().equals("")) {
return false; } return true; } / * * @描述 校验key与secret,mobiles[] * @参数 @param appId * @参数 @param secretKey * @参数 @param mobiles * @参数 @return * @return Boolean */ private static Boolean checkKeyParam(String appId, String secretKey, String[] mobiles) {
if (null == appId || secretKey == null || mobiles == null || appId.trim().equals("") || secretKey.trim().equals("") || mobiles.length == 0) {
return false; } return true; } / * 公共请求方法 */ public static ResultModel request(String appId, String secretKey, String algorithm, Object content, String url, final boolean isGzip, String encode) {
Map<String, String> headers = new HashMap<String, String>(); HttpRequest<byte[]> request = null; try {
headers.put("appId", appId); headers.put("encode", encode); String requestJson = JsonHelper.toJsonString(content); System.out.println("result json: " + requestJson); byte[] bytes = requestJson.getBytes(encode); System.out.println("request data size : " + bytes.length); if (isGzip) {
headers.put("gzip", "on"); bytes = GZIPUtils.compress(bytes); System.out.println("request data size [com]: " + bytes.length); } byte[] parambytes = AES.encrypt(bytes, secretKey.getBytes(), algorithm); System.out.println("request data size [en] : " + parambytes.length); HttpRequestParams<byte[]> params = new HttpRequestParams<byte[]>(); params.setCharSet("UTF-8"); params.setMethod("POST"); params.setHeaders(headers); params.setParams(parambytes); params.setUrl(url); if (url.startsWith("https://")) {
request = new HttpsRequestBytes(params, null); } else {
request = new HttpRequestBytes(params); } } catch (Exception e) {
System.out.println("加密异常"); e.printStackTrace(); } HttpClient client = new HttpClient(); String code = null; String result = null; try {
HttpResponseBytes res = client.service(request, new HttpResponseBytesPraser()); if (res == null) {
System.out.println("请求接口异常"); return new ResultModel(code, result); } if (res.getResultCode().equals(HttpResultCode.SUCCESS)) {
if (res.getHttpCode() == 200) {
code = res.getHeaders().get("result"); if (code.equals("SUCCESS")) {
byte[] data = res.getResult(); System.out.println("response data size [en and com] : " + data.length); data = AES.decrypt(data, secretKey.getBytes(), algorithm); if (isGzip) {
data = GZIPUtils.decompress(data); } System.out.println("response data size : " + data.length); result = new String(data, encode); System.out.println("response json: " + result); } } else {
System.out.println("请求接口异常,请求码:" + res.getHttpCode()); } } else {
System.out.println("请求接口网络异常:" + res.getResultCode().getCode()); } } catch (Exception e) {
System.out.println("解析失败"); e.printStackTrace(); } ResultModel re = new ResultModel(code, result); return re; } / * * @描述 获取剩余短信条数 * @参数 @param appId * @参数 @param secretKey * @参数 @return * @return Long */ public static Long getBalance(String appId, String secretKey) {
Boolean flag = checkKeyParam(appId, secretKey); if (flag) {
System.out.println("=============begin getBalance=================="); BalanceRequest pamars = new BalanceRequest(); ResultModel result = request(appId, secretKey, algorithm, pamars, host + "/inter/getBalance", isGzip, encode); System.out.println("result code :" + result.getCode()); if ("SUCCESS".equals(result.getCode())) {
BalanceResponse response = JsonHelper.fromJson(BalanceResponse.class, result.getResult()); if (response != null) {
System.out.println("result data : " + response.getBalance()); System.out.println("=============end getBalance=================="); return response.getBalance(); } } System.out.println("=============end getBalance=================="); return null; } else {
System.out.println("=============param check error=================="); return null; } } public static void main(String[] args) {
Long balance = getBalance(appId, secretKey); System.out.print("====剩余短信条数: "+"\033[0;31m"+balance+"条"); } }
看这demo写的话,其实很容易。有需要的可以私聊我!!!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/129620.html