闪速码可以免费试用200条的短信平台,可以用来发送短信验证码或者发送通知短信。平台不仅支持企业,还支持个人(我最看重这点了)。签名审核非常宽松,没有备案或者暂未上线的产品也可以申请签名
今天用闪速码为例,打包封装了java的短信接口,可在官网下载也可以直接使用,
1、test.java
// +----------------------------------------------------------------------------
// | 闪速码java短信sdk
// +----------------------------------------------------------------------------
// | Copyright (c) 2014-2021 https://www.shansuma.com
// +----------------------------------------------------------------------------
// | 闪速码短信平台支持网页在线发送、API集成发送、私有化部署三种形式,可免费试用200条
// +----------------------------------------------------------------------------
// | Author: 橘子俊,开发文档请访问 http://sms.shansuma.com/docs
// +----------------------------------------------------------------------------
package tech.haowei.sms;
public class TestClient {
public static void main(String[] args) {
Client client = new Client();
client.setAppId("hw_10902"); //开发者ID,在【设置】-【开发设置】中获取
client.setSecretKey("bf80b2a52abb94a4fbeb7c61015bf713"); //开发者密钥,在【设置】-【开发设置】中获取
client.setVersion("1.0");
/**
* json格式可在 bejson.com 进行校验
*/
String singnstr = "山顶烤肉";
Client.Request request = new Client.Request();
request.setMethod("sms.message.send");
request.setBizContent("{\"mobile\":[\"18616856295\"],\"type\":0,\"template_id\":\"ST_2020101100000003\",\"sign\":\"" + singnstr +"\",\"send_time\":\"\",\"params\":{\"code\":1234}}"); // 这里是json字符串,send_time 为空时可以为null, params 为空时可以为null,短信签名填写审核后的签名本身,不需要填写签名id
System.out.println( client.execute(request) );
}
}
2、clielnt.java
package tech.haowei.sms;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public class Client {
private String appId;
private long timestamp;
private String secretKey;
private String version;
private String signType;
Client() {
this.timestamp = System.currentTimeMillis();
this.version = "1.0";
this.signType = "md5";
}
public void setAppId(String appId) {
this.appId = appId;
}
public void setSignType(String signType) {
this.signType = signType;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public void setVersion(String version) {
this.version = version;
}
public static String md5(String s) {
char str[] = new char[32];
char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
try {
MessageDigest md = MessageDigest.getInstance("md5");
byte[] b = md.digest(s.getBytes());
int k = 0;
for (int i = 0; i < b.length; i++) {
str[k++] = hex[b[i] >>> 4 & 0xf];
str[k++] = hex[b[i] & 0xf];
}
} catch (Exception ex) {
ex.printStackTrace();
}
return new String(str);
}
public String createSignature(HashMap data, String secretKey) {
Object[] array = data.keySet().toArray();
Arrays.sort(array);
ArrayList list = new ArrayList<>();
for (Object key : array) {
list.add(key + "=" + data.get(key));
}
list.add("key=" + secretKey);
StringBuilder sb = new StringBuilder();
for (String v : list) {
sb.append(v);
sb.append("&");
}
sb.deleteCharAt(sb.length() - 1);
return md5(sb.toString()).toUpperCase();
}
public String execute(Request request) {
StringBuilder sb = new StringBuilder();
HashMap post = new HashMap<>();
post.put("app_id", this.appId);
post.put("timestamp", this.timestamp);
post.put("sign_type", this.signType);
post.put("version", this.version);
post.put("method", request.getMethod());
post.put("biz_content", request.getBizContent());
post.put("sign", createSignature(post, this.secretKey));
ArrayList list = new ArrayList<>();
for (String key : post.keySet()) {
list.add(key + "=" + post.get(key));
}
StringBuilder data = new StringBuilder();
for (String v : list) {
data.append(v);
data.append("&");
}
data.deleteCharAt(data.length() - 1);
//System.out.println(data.toString());
try {
URL url = new URL("http://api.shansuma.com/gateway.do");
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setUseCaches(false);
http.setConnectTimeout(15 * 1000);
http.setReadTimeout(60 * 1000);
http.setRequestMethod("POST");
http.setRequestProperty("User-Agent", "Mozilla 5.0 Java-SMS-SDK v1.0.0 (Haowei Tech)");
http.setRequestProperty("Connection", "close");
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
http.setRequestProperty("Content-Length", "" + data.length());
http.setDoOutput(true);
http.setDoInput(true);
OutputStream ops = http.getOutputStream();
ops.write(data.toString().getBytes());
ops.flush();
ops.close();
String next;
InputStreamReader reader = new InputStreamReader(http.getInputStream());
BufferedReader buffered = new BufferedReader(reader);
while ((next = buffered.readLine()) != null) {
sb.append(next);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return sb.toString();
}
public static class Request {
private String bizContent;
private String method;
public void setBizContent(String bizContent) {
this.bizContent = bizContent;
}
public void setMethod(String method) {
this.method = method;
}
public String getMethod() {
return method;
}
public String getBizContent() {
return bizContent;
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)