根据项目及客户需求,集成华软通信短信网关,实现短信发送功能,具体实现流程如下如下:
1、登录华软通信能力平台,配置相关免审核短信模板
在配置的时候,需要注意的是,如果短信内容中包含有敏感字符,比如“冻结”、违规、涉嫌…需要将内容隔开一点,“冻结”应该改为“冻 结”,“涉嫌”改为“涉 嫌”。提交本公司相关资质证明材料后,华软通信后台管理员审核通过就可以使用相关模板进行断线发送了。
补充说明:
- s表示支持的最多字符个数,例如s6就是短信内容最多可以包含6个字符
- 模板中的字符统一使用中文字符,不要使用英文状态下的字符,否则短信发送不成功(例如:逗号要使用中文状态下的,而不是英文状态下的,)
2、编写工具类实现短信发送
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.util.Map;
import java.util.TreeMap;
/**
* @Description: 华软通信-短信
* @Date: 2021-03-22
* @Author: Jonathan.WQ
* @Version V1.0
* @Modified By:
*/
public class SmsCommonHR {
private static final String URL = "http://jk.smstcby.com/smsGBK.aspx";
//短信前的签名,例如:【阿里巴巴】
private static final String NOTE_START_SIGN = "【】";
//发短信的账号,例如:15566667777
private static final String USERNAME = "";
//发短信密码对应的MD5值(登录密码),为了安全起见,建议直接加密后放在这里,不要调用下面的生成MD5值方法
private static final String PASSWORD = "44E6FA85C67E755A86C313BEFCAC6F55";
//短信网关ID
private static final String GATEWAY_ID = "";
/**
* @param data 短信内容
* @param mobile 号码
* @return
*/
public static String sendOne(String data, String mobile) {
StringBuilder sBuilder = new StringBuilder();
if (!data.startsWith(NOTE_START_SIGN)) {
data = NOTE_START_SIGN + data;
}
sBuilder.append("type=send&")
.append("username=" + USERNAME + "&")
.append("password=" + PASSWORD + "&")
.append("gwid=" + GATEWAY_ID + "&")
.append("mobile=" + mobile + "&")
.append("message=" + data);
String result = sendPost(URL, sBuilder.toString());
return result;
}
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
public static String getMD5String(String rawString) { //用来计算MD5的函数
String[] hexArray = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(rawString.getBytes());
byte[] rawBit = md.digest();
String outputMD5 = " ";
for (int i = 0; i < 16; i++) {
outputMD5 = outputMD5 + hexArray[rawBit[i] >>> 4 & 0x0f];
outputMD5 = outputMD5 + hexArray[rawBit[i] & 0x0f];
}
return outputMD5.trim();
} catch (Exception e) {
System.out.println("计算MD5值发生错误");
e.printStackTrace();
}
return null;
}
/**
* 生成秘钥
*
* @param tm
* @param key
* @return
*/
public static String createSign(TreeMap<String, String> tm, String key) {
StringBuffer buf = new StringBuffer(key);
for (Map.Entry<String, String> en : tm.entrySet()) {
String name = en.getKey();
String value = en.getValue();
if (!"sign".equals(name) && !"param".equals(name) && value != null && value.length() > 0 && !"null".equals(value)) {
buf.append(name).append('=').append(value).append('&');
}
}
String _buf = buf.toString();
return _buf.substring(0, _buf.length() - 1);
}
/**
* 将文件转成base64 字符串
*
* @param path 文件路径
* @return *
* @throws Exception
*/
public static String encodeBase64File(String path) throws Exception {
File file = new File(path);
;
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer);
inputFile.close();
//return new BASE64Encoder().encode(buffer);
return "";
}
}
3、测试
/**
* 测试
*
* @param args
*/
public static void main(String[] args) {
String verification_code= NOTE_START_SIGN + "你正在使用手机号修改用户名,验证码为:{code}" + ",验证码5分钟内有效(请勿泄露,如非本人操作请忽略)";//短信验证码例子
//System.out.println(sendMsg(VERIFICATION_CODE.replace("{code}", "5925"), "15566667777"));
System.out.println(sendOne(verification_code.replace("{code}", "5925"), "15566667777"));
}
备注:发送短信签名的时候,必须带上签名,否则可能发送不成功
发送结果: