短信平台API接口demo示例-JAVA/Message/MultiSend
DEMO:Message/MultiSend
原文链接
- 支持JDK版本:1.5以上
- 依赖的jar包:httpclient-4.5.3.jar、httpcore-4.4.14.jar、commons-logging1.1.1.jar、fastjson-1.2.75.jar
org.apache.httpcomponents
httpclient
4.5.3
org.apache.httpcomponents
httpcore
4.4.14
commons-logging
commons-logging
1.1.1
com.alibaba
fastjson
1.2.75
【短信平台API接口demo示例-JAVA/Message/MultiSend】
代码示列 MessageMultiSendDemo
package com.submail.demo.message;
import java.io.IOException;
import java.util.TreeMap;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.submail.demo.RequestEncoder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
public class MessageMultiSendDemo {
public static final String TIMESTAMP = "https://api.mysubmail.com/service/timestamp";
private static final String URL = "https://api.mysubmail.com/message/multisend.json";
public static final String TYPE_MD5 = "md5";
public static final String TYPE_SHA1 = "sha1";
public static void main(String[] args) {
TreeMap requestData = https://www.it610.com/article/new TreeMap();
String appid ="";
String appkey = "";
String content = "【xxx】@var(name),你的验证码是:@var(code)";
String sign_type = "normal";
String sign_version = "2";
//组合请求数据
requestData.put("appid", appid);
JSONObject var1 = new JSONObject();
String to1 = "176xxxx5149";
var1.put("name", "张无忌");
var1.put("code", "123452");
JSONObject var2 = new JSONObject();
String to2 = "176xxxx5149";
var2.put("name", "张三丰");
var2.put("code", "888881");
Multi multi=new Multi();
multi.add(to1,var1);
multi.add(to2,var2);
if (sign_type.equals(TYPE_MD5) || sign_type.equals(TYPE_SHA1)) {
String timestamp = getTimestamp();
requestData.put("timestamp", timestamp);
requestData.put("sign_type", sign_type);
requestData.put("sign_version", sign_version);
String signStr = appid + appkey + RequestEncoder.formatRequest(requestData) + appid + appkey;
requestData.put("signature", RequestEncoder.encode(sign_type, signStr));
} else {
requestData.put("signature", appkey);
}
requestData.put("content", content);
requestData.put("multi",multi.toJSONString());
//post请求
HttpPost httpPost = new HttpPost(URL);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");
StringEntity entity = new StringEntity(JSONObject.toJSONString(requestData), "UTF-8");
httpPost.setEntity(entity);
try {
CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
HttpResponse response = closeableHttpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
String jsonStr = EntityUtils.toString(httpEntity, "UTF-8");
System.out.println(jsonStr);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}//获取时间戳
private static String getTimestamp() {
CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
HttpGet httpget = new HttpGet(TIMESTAMP);
try {
HttpResponse response = closeableHttpClient.execute(httpget);
HttpEntity httpEntity = response.getEntity();
String jsonStr = EntityUtils.toString(httpEntity, "UTF-8");
if (jsonStr != null) {
JSONObject json = JSONObject.parseObject(jsonStr);
return json.getString("timestamp");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}class Multi{
private JSONArray jsonArray;
public Multi(){
jsonArray=new JSONArray();
}public void add(String to,JSONObject vars){
JSONObject jobject=new JSONObject();
jobject.put("to",to);
jobject.put("vars",vars);
jsonArray.add(jobject);
}public String toJSONString(){
return jsonArray.toJSONString();
}
}
RequestEncoder
package com.submail.demo;
/**
* @author zhang
* @date 2021/8/4 - 5:52 下午
*/import java.security.MessageDigest;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class RequestEncoder {
private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
public static String encode(String algorithm, String str) {
if (str == null) {
return null;
}
try {
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
messageDigest.update(str.getBytes("UTF-8"));
return getFormattedText(messageDigest.digest());
} catch (Exception e) {
throw new RuntimeException(e);
}}private static String getFormattedText(byte[] bytes) {
int len = bytes.length;
StringBuilder buf = new StringBuilder(len * 2);
for (int j = 0;
j < len;
j++) {
buf.append(HEX_DIGITS[(bytes[j] >> 4) &
0x0f]);
buf.append(HEX_DIGITS[bytes[j] &
0x0f]);
}
return buf.toString();
}public static String formatRequest(Map data) {
Set keySet = data.keySet();
Iterator it = keySet.iterator();
StringBuffer sb = new StringBuffer();
while (it.hasNext()) {
String key = it.next();
Object value = https://www.it610.com/article/data.get(key);
if (value instanceof String) {
sb.append(key +"=" + value + "&
");
}
}
if (sb.length() != 0) {
System.out.println("sb.substring(0, sb.length() - 1) = " + sb.substring(0, sb.length() - 1));
return sb.substring(0, sb.length() - 1);
}
return null;
}
}
推荐阅读
- 短信平台API接口demo示例-JAVA/Message/MultiXSend
- 极客日报|孟晚舟升任华为轮值董事长;腾讯云多媒体实验室复原张国荣《热·情》演唱会;世界首款Lightning接口安卓手机|极客头条
- 自然语言处理|纵览200大规模机器学习研究!道翰天琼认知智能机器人平台API接口大脑为您揭秘-3。
- 纵览200大规模机器学习研究!道翰天琼认知智能机器人平台API接口大脑为您揭秘。
- (十)React|(十)React Ant Design Pro + .Net5 WebApi(后端环境搭建-IdentityServer4(二)授权模式)
- APIService|Spring2.5.2 经典在线教程—https://docs.spring.io/spring/docs/
- opencv——OpenCV中的滑动条
- Mybatis|Mybatis-Plus入门(Mapper CRUD接口)
- mybatis|Mybatis-plus的Mapper CRUD 接口查询数据错误
- 序列化多表操作、请求与响应、视图组件(子类与拓展类)、继承GenericAPIView类重写接口