Browse Source

政务云短信api对接

master
21819 2 years ago
parent
commit
07e9a11b19
  1. 12
      mailbox-outer/src/main/java/com/biutag/outer/domain/vo/ReqImsApi.java
  2. 12
      mailbox-outer/src/main/java/com/biutag/outer/domain/vo/balance/JsonQueryBalance.java
  3. 14
      mailbox-outer/src/main/java/com/biutag/outer/domain/vo/mo/JsonMo.java
  4. 12
      mailbox-outer/src/main/java/com/biutag/outer/domain/vo/mo/ReqMo.java
  5. 14
      mailbox-outer/src/main/java/com/biutag/outer/domain/vo/mo/SmsMo.java
  6. 12
      mailbox-outer/src/main/java/com/biutag/outer/domain/vo/mt/JsonSmsSend.java
  7. 12
      mailbox-outer/src/main/java/com/biutag/outer/domain/vo/mt/Mobile.java
  8. 15
      mailbox-outer/src/main/java/com/biutag/outer/domain/vo/mt/SendSms.java
  9. 14
      mailbox-outer/src/main/java/com/biutag/outer/domain/vo/rpt/JsonRpt.java
  10. 12
      mailbox-outer/src/main/java/com/biutag/outer/domain/vo/rpt/ReqRpt.java
  11. 14
      mailbox-outer/src/main/java/com/biutag/outer/domain/vo/rpt/SmsRpt.java
  12. 55
      mailbox-outer/src/main/java/com/biutag/outer/util/JsonUtil.java
  13. 30
      mailbox-outer/src/main/java/com/biutag/outer/util/Md5Util.java
  14. 39
      mailbox-outer/src/main/java/com/biutag/outer/util/MoSync.java
  15. 39
      mailbox-outer/src/main/java/com/biutag/outer/util/RptSync.java
  16. 82
      mailbox-outer/src/main/java/com/biutag/outer/util/SmsSend.java
  17. 49
      mailbox-outer/src/main/java/com/biutag/outer/util/sms/Base64Util.java
  18. 56
      mailbox-outer/src/main/java/com/biutag/outer/util/sms/JsonUtil.java
  19. 30
      mailbox-outer/src/main/java/com/biutag/outer/util/sms/Md5Util.java
  20. 188
      mailbox-outer/src/main/java/com/biutag/outer/util/sms/PostSms.java
  21. 32
      mailbox-outer/src/main/java/com/biutag/outer/util/sms/SmsConf.java
  22. 87
      mailbox-outer/src/main/java/com/biutag/outer/util/sms/SmsHttp.java
  23. 1
      mailbox-outer/src/test/java/SmsTest.java

12
mailbox-outer/src/main/java/com/biutag/outer/domain/vo/ReqImsApi.java

@ -0,0 +1,12 @@
package com.biutag.outer.domain.vo;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class ReqImsApi {
private String userName;//用户名
private String signStr;//签名字符
private String signTime;//提交时间
}

12
mailbox-outer/src/main/java/com/biutag/outer/domain/vo/balance/JsonQueryBalance.java

@ -0,0 +1,12 @@
package com.biutag.outer.domain.vo.balance;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class JsonQueryBalance {
private int state = -999;//0 正常 -1通用异常 -999程序EXCEPTION -998非JSON字符
private String message = "服务器忙,响应超时 ";
private int balance;
}

14
mailbox-outer/src/main/java/com/biutag/outer/domain/vo/mo/JsonMo.java

@ -0,0 +1,14 @@
package com.biutag.outer.domain.vo.mo;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
public class JsonMo {
private int state = -1;
private String message = "系统超时";
private List<SmsMo> object;
}

12
mailbox-outer/src/main/java/com/biutag/outer/domain/vo/mo/ReqMo.java

@ -0,0 +1,12 @@
package com.biutag.outer.domain.vo.mo;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class ReqMo {
private String userName;
private String signStr;
private String signTime;
}

14
mailbox-outer/src/main/java/com/biutag/outer/domain/vo/mo/SmsMo.java

@ -0,0 +1,14 @@
package com.biutag.outer.domain.vo.mo;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class SmsMo {
private String moId;//上行ID
private String mobileNo;//上行电话号码
private String spCode;//子码
private String moStr;//上行内容
private String moTime;//上行时
}

12
mailbox-outer/src/main/java/com/biutag/outer/domain/vo/mt/JsonSmsSend.java

@ -0,0 +1,12 @@
package com.biutag.outer.domain.vo.mt;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class JsonSmsSend {
private int state=-1;
private String message="系统超时";
private String smsId;
}

12
mailbox-outer/src/main/java/com/biutag/outer/domain/vo/mt/Mobile.java

@ -0,0 +1,12 @@
package com.biutag.outer.domain.vo.mt;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
@Accessors(chain = true)
@Getter
@Setter
public class Mobile {
private String mobile;//接收单个手机号码
}

15
mailbox-outer/src/main/java/com/biutag/outer/domain/vo/mt/SendSms.java

@ -0,0 +1,15 @@
package com.biutag.outer.domain.vo.mt;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
public class SendSms {
private String userName;//用户名
private String signStr;//签名字符
private String signTime;//提交时间
private String smsStr;//短信内容转BASE64
private List<Mobile> mobileList;//发送号码列表 最多2000个
}

14
mailbox-outer/src/main/java/com/biutag/outer/domain/vo/rpt/JsonRpt.java

@ -0,0 +1,14 @@
package com.biutag.outer.domain.vo.rpt;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
public class JsonRpt {
private int state=-1;
private String message="系统超时";
private List<SmsRpt> object;
}

12
mailbox-outer/src/main/java/com/biutag/outer/domain/vo/rpt/ReqRpt.java

@ -0,0 +1,12 @@
package com.biutag.outer.domain.vo.rpt;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class ReqRpt {
private String userName;
private String signStr;
private String signTime;
}

14
mailbox-outer/src/main/java/com/biutag/outer/domain/vo/rpt/SmsRpt.java

@ -0,0 +1,14 @@
package com.biutag.outer.domain.vo.rpt;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class SmsRpt {
private String smsId;//任务ID
private String mobileNo;//号码
private int rptState;//发送状态
private String rptNote;//回执备注
private String rptTime;//回执时间
}

55
mailbox-outer/src/main/java/com/biutag/outer/util/JsonUtil.java

@ -1,55 +0,0 @@
package com.biutag.outer.util;
import com.google.gson.*;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class JsonUtil {
public static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
public static String toJson(Object msg) {
return gson.toJson(msg);
}
public static <T> T returnObj(String json,Class<T> clazz){
return gson.fromJson(json, clazz);
}
public static <T> T returnObj(String json, Type type){
return gson.fromJson(json, type);
}
//验证是不是JSON字符串
public static boolean isJsonStr(String jsonInString) {
try {
if (jsonInString!=null && !jsonInString.isEmpty()){
gson.fromJson(jsonInString, Object.class);
return true;
}else {
return false;
}
} catch(JsonSyntaxException ex) {
return false;
}
}
public static <T> List<T> toList(String string,Class<T> clz){
if(string == null || string.length() < 1){
return null;
}else {
List<T> list = new ArrayList<>();
if(!string.startsWith("[")){
string = "[" + string +"]";
}
JsonArray array = new JsonParser().parse(string).getAsJsonArray();
for(final JsonElement elem : array){
if(elem != null){
String mo = elem.toString().replace("\"{","{").replace("}\"","}").replace("\\", "");
list.add(gson.fromJson(mo, clz));
}
}
return list;
}
}
}

30
mailbox-outer/src/main/java/com/biutag/outer/util/Md5Util.java

@ -1,30 +0,0 @@
package com.biutag.outer.util;
import java.security.MessageDigest;
public class Md5Util {
public static final String strToMD5(String s) {
char[] hexDigits = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
'f' };
try {
byte[] btInput = s.getBytes("UTF-8");
MessageDigest mdInst = MessageDigest.getInstance("MD5");
mdInst.update(btInput);
byte[] md = mdInst.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 & 15];
str[k++] = hexDigits[byte0 & 15];
}
return new String(str);
} catch (Exception var10) {
var10.printStackTrace();
return null;
}
}
}

39
mailbox-outer/src/main/java/com/biutag/outer/util/MoSync.java

@ -0,0 +1,39 @@
package com.biutag.outer.util;
import com.biutag.outer.domain.vo.mo.JsonMo;
import com.biutag.outer.domain.vo.mo.ReqMo;
import com.biutag.outer.util.sms.JsonUtil;
import com.biutag.outer.util.sms.Md5Util;
import com.biutag.outer.util.sms.SmsConf;
import com.biutag.outer.util.sms.SmsHttp;
public class MoSync {
//拿取上行
public static void main(String args[]) {
System.out.println("取上行启动成功");
try {
String userName = SmsConf.smsUserName;
String password = SmsConf.smsPasswd;
String signTime = SmsConf.getSysTime();
String signStr = Md5Util.strToMD5(userName + Md5Util.strToMD5(password) + signTime);
ReqMo reqMo = new ReqMo();
reqMo.setSignStr(signStr);
reqMo.setSignTime(signTime);
reqMo.setUserName(userName);
String reqJsonStr = JsonUtil.toJson(reqMo);
String resultJsonStr = SmsHttp.postJson(SmsConf.smsMoUrl, reqJsonStr);
if (JsonUtil.isJsonStr(resultJsonStr)) {
JsonMo jsonMo = JsonUtil.returnObj(resultJsonStr, JsonMo.class);
if(jsonMo!=null){
//进入数据处理流程
//saveMo(jsonRpt);
}
} else {
System.out.println("取回执:"+resultJsonStr);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

39
mailbox-outer/src/main/java/com/biutag/outer/util/RptSync.java

@ -0,0 +1,39 @@
package com.biutag.outer.util;
import com.biutag.outer.domain.vo.rpt.JsonRpt;
import com.biutag.outer.domain.vo.rpt.ReqRpt;
import com.biutag.outer.util.sms.JsonUtil;
import com.biutag.outer.util.sms.Md5Util;
import com.biutag.outer.util.sms.SmsConf;
import com.biutag.outer.util.sms.SmsHttp;
public class RptSync {
//拿取回执
public static void main(String args[]) {
System.out.println("取回执启动成功");
try {
String userName = SmsConf.smsUserName;
String password = SmsConf.smsPasswd;
String signTime = SmsConf.getSysTime();
String signStr = Md5Util.strToMD5(userName + Md5Util.strToMD5(password) + signTime);
ReqRpt reqRpt = new ReqRpt();
reqRpt.setSignStr(signStr);
reqRpt.setSignTime(signTime);
reqRpt.setUserName(userName);
String reqJsonStr = JsonUtil.toJson(reqRpt);
String resultJsonStr = SmsHttp.postJson(SmsConf.smsRptUrl, reqJsonStr);
if (JsonUtil.isJsonStr(resultJsonStr)) {
JsonRpt jsonRpt = JsonUtil.returnObj(resultJsonStr, JsonRpt.class);
if(jsonRpt!=null){
//进入数据处理流程
//saveRpt(jsonRpt);
}
} else {
System.out.println("取回执:"+resultJsonStr);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

82
mailbox-outer/src/main/java/com/biutag/outer/util/SmsSend.java

@ -6,38 +6,78 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.biutag.outer.domain.vo.mt.JsonSmsSend;
import com.biutag.outer.domain.vo.mt.Mobile;
import com.biutag.outer.util.sms.PostSms;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
@Slf4j
public class SmsSend {
//调起发送程序
public static void send(String phoneNumber, String code) {
String url = "http://172.31.253.178:5050/ims/sms/sendSms";
String userName = "cxxjjjbxt";
String password = "rfrlnK@98";
String signTime = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_FORMAT);
String signStr = Md5Util.strToMD5(userName+Md5Util.strToMD5(password)+signTime);
ReqImsApi reqImsApi = new ReqImsApi();
reqImsApi.setUserName(userName);
reqImsApi.setSignStr(signStr);
reqImsApi.setSignTime(signTime);
reqImsApi.setSmsStr(String.format("您的验证码为:%s,请勿泄露于他人!", code));
reqImsApi.setMobileList(Collections.singletonList(JSONObject.of("mobile", phoneNumber)));
String json = JsonUtil.toJson(reqImsApi);
/**
* 发送单个短信短信
*
* @param phone 单个手机号码
* @param content 短信内容
*/
public static void sendSms(String phone, String content) {
try {
HttpResponse response = HttpUtil.createPost(url).body(json).execute();
//发送
Mobile mobile = new Mobile();
mobile.setMobile(phone);
List<Mobile> mobileList = new ArrayList<Mobile>();
mobileList.add(mobile);
//更新提交信息
JsonSmsSend jsonSmsSend = PostSms.sendSms(mobileList, content);
if (jsonSmsSend != null) {
if (jsonSmsSend.getState() == 0) {
log.info("短信发送成功,手机号:{},内容:{}", phone, content);
} else {
log.info(jsonSmsSend.getMessage());
}
} else {
log.info("发送返回空");
}
log.info("手机号:{}, 发送短信结果:{}", phoneNumber, response.body());
} catch (RuntimeException e) {
log.error(e.getMessage(), e);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 发送多个短信短信
*
* @param phoneList 手机号码列表
* @param content 短信内容
*/
public static void sendSms(List<String> phoneList, String content) {
try {
List<Mobile> mobileList = new ArrayList<>();
for (String phone : phoneList) {
Mobile mobile = new Mobile();
mobile.setMobile(phone);
mobileList.add(mobile);
}
//更新提交信息
JsonSmsSend jsonSmsSend = PostSms.sendSms(mobileList, content);
if (jsonSmsSend != null) {
if (jsonSmsSend.getState() == 0) {
log.info("短信发送成功,手机号:{},内容:{}", phoneList, content);
} else {
log.info(jsonSmsSend.getMessage());
}
} else {
log.info("发送返回空");
}
} catch (Exception e) {
log.error("发送短信异常", e);
}
}
}

49
mailbox-outer/src/main/java/com/biutag/outer/util/sms/Base64Util.java

@ -0,0 +1,49 @@
package com.biutag.outer.util.sms;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Base64Util {
public static String encodeData(String str) {
//L.p("str-->"+str);
byte[] b = null;
String s = null;
if (str != null && !str.trim().isEmpty()) {
b = str.getBytes(StandardCharsets.UTF_8);
if (b != null) {
s = Base64.getMimeEncoder().encodeToString(b);
}
}
return replaceBlank(s);
}
// 解密
public static String decodeData(String s) {
byte[] b = null;
String result = null;
if (s != null) {
try {
b = Base64.getMimeDecoder().decode(s);
result = new String(b, StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
//去掉换行符
public static String replaceBlank(String str) {
String dest = "";
if (str != null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(str);
dest = m.replaceAll("");
}
return dest;
}
}

56
mailbox-outer/src/main/java/com/biutag/outer/util/sms/JsonUtil.java

@ -0,0 +1,56 @@
package com.biutag.outer.util.sms;
import com.google.gson.*;
import java.util.ArrayList;
import java.util.List;
import java.lang.reflect.Type;
public class JsonUtil {
public static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
public static String toJson(Object msg) {
return gson.toJson(msg);
}
public static <T> T returnObj(String json, Class<T> clazz) {
return gson.fromJson(json, clazz);
}
public static <T> T returnObj(String json, Type type) {
return gson.fromJson(json, type);
}
//验证是不是JSON字符串
public static boolean isJsonStr(String jsonInString) {
try {
if (jsonInString != null && !jsonInString.isEmpty()) {
gson.fromJson(jsonInString, Object.class);
return true;
} else {
return false;
}
} catch (JsonSyntaxException ex) {
return false;
}
}
public static <T> List<T> toList(String string, Class<T> clz) {
if (string == null || string.length() < 1) {
return null;
} else {
List<T> list = new ArrayList<>();
if (!string.startsWith("[")) {
string = "[" + string + "]";
}
JsonArray array = new JsonParser().parse(string).getAsJsonArray();
for (final JsonElement elem : array) {
if (elem != null) {
String mo = elem.toString().replace("\"{", "{").replace("}\"", "}").replace("\\", "");
list.add(gson.fromJson(mo, clz));
}
}
return list;
}
}
}

30
mailbox-outer/src/main/java/com/biutag/outer/util/sms/Md5Util.java

@ -0,0 +1,30 @@
package com.biutag.outer.util.sms;
import java.security.MessageDigest;
public class Md5Util {
public static final String strToMD5(String s) {
char[] hexDigits = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
'f' };
try {
byte[] btInput = s.getBytes("UTF-8");
MessageDigest mdInst = MessageDigest.getInstance("MD5");
mdInst.update(btInput);
byte[] md = mdInst.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 & 15];
str[k++] = hexDigits[byte0 & 15];
}
return new String(str);
} catch (Exception var10) {
var10.printStackTrace();
return null;
}
}
}

188
mailbox-outer/src/main/java/com/biutag/outer/util/sms/PostSms.java

@ -0,0 +1,188 @@
package com.biutag.outer.util.sms;
import com.biutag.outer.domain.vo.balance.JsonQueryBalance;
import com.biutag.outer.domain.vo.mo.JsonMo;
import com.biutag.outer.domain.vo.mt.JsonSmsSend;
import com.biutag.outer.domain.vo.mt.Mobile;
import com.biutag.outer.domain.vo.mt.SendSms;
import com.biutag.outer.domain.vo.rpt.JsonRpt;
import com.biutag.outer.util.ReqImsApi;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class PostSms {
//查询发送
public static int queryBalanceNum() {
String userName = SmsConf.smsUserName;
String password = SmsConf.smsPasswd;
String signTime = SmsConf.getSysTime();
int balanceNum = 0;
try {
String signStr = Md5Util.strToMD5(userName + Md5Util.strToMD5(password) + signTime);
ReqImsApi reqImsApi = new ReqImsApi();
reqImsApi.setUserName(userName);
reqImsApi.setSignStr(signStr);
reqImsApi.setSignTime(signTime);
String reqJsonStr = JsonUtil.toJson(reqImsApi);
String resultJsonStr = SmsHttp.postJson(SmsConf.queyrbalanceUrl, reqJsonStr);
if (JsonUtil.isJsonStr(resultJsonStr)) {
JsonQueryBalance jonQueryBalance = JsonUtil.returnObj(resultJsonStr, JsonQueryBalance.class);
if (jonQueryBalance != null) {
balanceNum = jonQueryBalance.getBalance();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return balanceNum;
}
//提交短信
public static JsonSmsSend sendSms(List<Mobile> mobileList, String smsStr) {
String userName = SmsConf.smsUserName;
String password = SmsConf.smsPasswd;
String signTime = SmsConf.getSysTime();
JsonSmsSend jsonSmsSend = new JsonSmsSend();
try {
String signStr = Md5Util.strToMD5(userName + Md5Util.strToMD5(password) + signTime);
if (smsStr != null && !smsStr.isEmpty()) {
if (mobileList != null && !mobileList.isEmpty()) {
if (PostSms.queryBalanceNum() >= mobileList.size()) {
SendSms sendSms = new SendSms();
sendSms.setUserName(userName);
sendSms.setSignStr(signStr);
sendSms.setSignTime(signTime);
sendSms.setSignStr(signStr);
sendSms.setSmsStr(Base64Util.encodeData(smsStr));
sendSms.setMobileList(mobileList);
String reqJsonStr = JsonUtil.toJson(sendSms);
String resultJsonStr = SmsHttp.postJson(SmsConf.smsSendUrl, reqJsonStr);
log.info("smsStr: " + smsStr);
log.info("smsStr: " + sendSms.getSmsStr());
log.info("decodeStr: " + Base64Util.decodeData(sendSms.getSmsStr()));
if (JsonUtil.isJsonStr(resultJsonStr)) {
jsonSmsSend = JsonUtil.returnObj(resultJsonStr, JsonSmsSend.class);
} else {
jsonSmsSend.setState(-1);
jsonSmsSend.setMessage(resultJsonStr);
}
} else {
jsonSmsSend.setState(-1);
jsonSmsSend.setMessage("账户余额不够");
}
} else {
jsonSmsSend.setState(-1);
jsonSmsSend.setMessage("发送号码不能空");
}
} else {
jsonSmsSend.setState(-1);
jsonSmsSend.setMessage("发送内容不能空");
}
} catch (Exception e) {
jsonSmsSend.setState(-1);
jsonSmsSend.setMessage(e.toString());
e.printStackTrace();
}
return jsonSmsSend;
}
//接收短信状态回执
public static JsonRpt getSmsRpt() {
String userName = SmsConf.smsUserName;
String password = SmsConf.smsPasswd;
String signTime = SmsConf.getSysTime();
JsonRpt jsonRpt = new JsonRpt();
try {
String signStr = Md5Util.strToMD5(userName + Md5Util.strToMD5(password) + signTime);
ReqImsApi reqImsApi = new ReqImsApi();
reqImsApi.setUserName(userName);
reqImsApi.setSignStr(signStr);
reqImsApi.setSignTime(signTime);
String reqJsonStr = JsonUtil.toJson(reqImsApi);
String resultJsonStr = SmsHttp.postJson(SmsConf.smsRptUrl, reqJsonStr);
if (JsonUtil.isJsonStr(resultJsonStr)) {
jsonRpt = JsonUtil.returnObj(resultJsonStr, JsonRpt.class);
} else {
jsonRpt.setState(-1);
jsonRpt.setMessage(resultJsonStr);
}
} catch (Exception e) {
jsonRpt.setMessage(e.toString());
e.printStackTrace();
}
return jsonRpt;
}
//接收短信上行
public static JsonMo getSmsMo() {
String userName = SmsConf.smsUserName;
String password = SmsConf.smsPasswd;
String signTime = SmsConf.getSysTime();
String balanceUrl = SmsConf.smsMoUrl;
JsonMo jsonMo = new JsonMo();
try {
String signStr = Md5Util.strToMD5(userName + Md5Util.strToMD5(password) + signTime);
ReqImsApi reqImsApi = new ReqImsApi();
reqImsApi.setUserName(userName);
reqImsApi.setSignStr(signStr);
reqImsApi.setSignTime(signTime);
String reqJsonStr = JsonUtil.toJson(reqImsApi);
String resultJsonStr = SmsHttp.postJson(balanceUrl, reqJsonStr);
if (JsonUtil.isJsonStr(resultJsonStr)) {
jsonMo = JsonUtil.returnObj(resultJsonStr, JsonMo.class);
} else {
jsonMo.setState(-1);
jsonMo.setMessage(resultJsonStr);
}
} catch (Exception e) {
jsonMo.setMessage(e.toString());
e.printStackTrace();
}
return jsonMo;
}
public static void main(String arg[]) throws Exception {
//测试取余额
//L.p(""+PostSms.queryBalanceNum());
//测试发送
List<Mobile> mobileList = new ArrayList<Mobile>();
Mobile mobile = new Mobile();
mobile.setMobile("13974908181");
mobileList.add(mobile);
String smsStr = "验证码1234";
JsonSmsSend jsonSmsSend = sendSms(mobileList, smsStr);
// if(jsonSmsSend!=null){
// L.p(jsonSmsSend.getSmsId());
// }
//测试取回执
// JsonRpt jsonRpt=getSmsRpt();
// L.p(""+jsonRpt.getMessage());
// List<SmsRpt> rptList=jsonRpt.getObject();
// if(rptList!=null && !rptList.isEmpty()){
// L.p(rptList.size()+"");
// }else{
// L.p("123");
// }
//测试取上行
// JsonMo jsonMo=getSmsMo();
// List<SmsMo> moList=jsonMo.getObject();
// System.out.println(""+jsonMo.getMessage());
// if(moList!=null && !moList.isEmpty()){
// System.out.println(""+moList.size());
// }else{
// System.out.println("123");
// }
}
}

32
mailbox-outer/src/main/java/com/biutag/outer/util/sms/SmsConf.java

@ -0,0 +1,32 @@
package com.biutag.outer.util.sms;
import java.util.Calendar;
public class SmsConf {
//基础地址 请参文档填写
public static String smsSendUrl = "http://172.31.253.178:5050/ims/sms/sendSms";//发送地址
public static String queyrbalanceUrl = "http://172.31.253.178:5050/ims/sms/queryBalance";//查询发送能力地址
public static String smsMoUrl = "http://172.31.253.178:9090/ims/sms/queryMo";//取上行地址
public static String smsRptUrl = "http://172.31.253.178:9090/ims/sms/queryRpt";//取回执地址
public static String smsUserName="cxxjjjbxt";//账户名
public static String smsPasswd="rfrlnK@98";//账号密码
public static String getSysTime() {
Calendar now = Calendar.getInstance();
String year = Integer.toString(now.get(Calendar.YEAR));
String mon = Integer.toString(now.get(Calendar.MONTH) + 1);
String day = Integer.toString(now.get(Calendar.DATE));
String hour = Integer.toString(now.get(Calendar.HOUR_OF_DAY));
String min = Integer.toString(now.get(Calendar.MINUTE));
String sec = Integer.toString(now.get(Calendar.SECOND));
mon = (mon.length() == 1) ? "0" + mon : mon;
day = (day.length() == 1) ? "0" + day : day;
hour = (hour.length() == 1) ? "0" + hour : hour;
min = (min.length() == 1) ? "0" + min : min;
sec = (sec.length() == 1) ? "0" + sec : sec;
return (year + mon + day + hour + min + sec);
}
}

87
mailbox-outer/src/main/java/com/biutag/outer/util/sms/SmsHttp.java

@ -0,0 +1,87 @@
package com.biutag.outer.util.sms;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class SmsHttp {
//发送生信
public static String postJson(String submitUrl, String json) {
URL url = null;
String postResponseStr = null;
HttpURLConnection httpURLConnection = null;
BufferedReader getResponseBytes = null;
PrintWriter printWriter = null;
StringBuilder sb = null;
try {
url = new URL(submitUrl);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// 提交模式
httpURLConnection.setConnectTimeout(10000);//连接超时 单位毫秒
httpURLConnection.setReadTimeout(60000);//读取超时 单位毫秒
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
//httpURLConnection.setRequestProperty("Accept", "application/json");
httpURLConnection.setRequestProperty("Content-Type", "text/plain");
httpURLConnection.setRequestProperty("connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charsert", "UTF-8");
//httpURLConnection.setRequestProperty("Content-type", "application/json; charset=utf-8");
// 获取URLConnection对象对应的输出流
printWriter = new PrintWriter(httpURLConnection.getOutputStream());
printWriter.write(json);
printWriter.flush();
//printWriter.close();
int httpRspCode = httpURLConnection.getResponseCode();
if (httpRspCode == HttpURLConnection.HTTP_OK) {
sb = new StringBuilder();
// 开始获取数据
getResponseBytes = new BufferedReader(
new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
String line = null;
while ((line = getResponseBytes.readLine()) != null) {
sb.append(line);
}
getResponseBytes.close();
postResponseStr = sb.toString();
} else {
postResponseStr = "ERR:" + httpRspCode;
}
} catch (Exception e) {
postResponseStr = "ERR:" + e.getMessage();
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
try {
httpURLConnection.disconnect();
} catch (Exception eee) {
}
}
if (getResponseBytes != null) {
try {
getResponseBytes.close();
} catch (Exception ee) {
}
}
if (printWriter != null) {
try {
printWriter.close();
} catch (Exception eeee) {
}
}
if (sb != null) {
try {
sb = null;
} catch (Exception eeeee) {
}
}
}
System.out.print("sms sends post-->[" + postResponseStr + "] json:" + json);
return postResponseStr;
}
}

1
mailbox-outer/src/test/java/SmsTest.java

@ -3,7 +3,6 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.biutag.outer.util.JsonUtil;
import com.biutag.outer.util.Md5Util;
import com.biutag.outer.util.ReqImsApi;

Loading…
Cancel
Save