短信平台API接口demo示例-JAVA/Message/XSend
DEMO:Message/XSend
原文链接
- 支持JDK版本:1.5以上
- 依赖的jar包:httpclient-4.5.3.jar、httpcore-4.4.14.jar、commons-logging1.1.1.jar、fastjson-1.2.75.jar
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.3</version>
</dependency>
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.4.14</version>
</dependency>
<dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1.1</version>
</dependency>
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.75</version>
</dependency>
代码示列
MessageXSendDemo
package com.submail.demo.message;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
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 MessageXsendDemo {public static final String TIMESTAMP = "https://api.mysubmail.com/service/timestamp";private static final String URL = "https://api.mysubmail.com/message/xsend";public static final String TYPE_MD5 = "md5";public static final String TYPE_SHA1 = "sha1";public static void main(String[] args) {TreeMap<String, String> requestData = new TreeMap<String, String>();JSONObject vars = new JSONObject();String appid = "";String appkey = "";String to = "176xxxx149";String project = "CtdN24";String sign_type = "md5";String sign_version = "2";vars.put("code", "3314");vars.put("name", "张三丰");//组合请求数据requestData.put("appid", appid);requestData.put("to", to);requestData.put("project", project);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;System.out.println(signStr);requestData.put("signature", RequestEncoder.encode(sign_type, signStr));} else {requestData.put("signature", appkey);}requestData.put("vars", vars.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;}
}
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<String, String> data) {Set<String> keySet = data.keySet();Iterator<String> it = keySet.iterator();StringBuffer sb = new StringBuffer();while (it.hasNext()) {String key = it.next();Object value = 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/XSend相关推荐
- 短信平台API接口demo示例-JAVA/Message/Send
DEMO:Message/Send 原文链接 支持JDK版本:1.5以上 依赖的jar包:httpclient-4.5.3.jar.httpcore-4.4.14.jar.commons-loggin ...
- 短信平台API接口demo示例-JAVA/Message/MultiXSend
DEMO:Message/MultiXSend 原文链接 支持JDK版本:1.5以上 依赖的jar包:httpclient-4.5.3.jar.httpcore-4.4.14.jar.commons- ...
- 短信平台API接口demo示例-Node/SMS/XSend
DEMO: SMS/XSend - 短信模板发送 原文链接 var request = require('request'); var crypto = require('crypto'); var ...
- 短信平台API接口demo示例-Node/SMS/Send
DEMO: SMS/Send - 短信发送 原文链接 var request = require('request'); var crypto = require('crypto'); var app ...
- 短信平台API接口demo示例-Node/SMS/MultiSend
DEMO: SMS/MultiSend - 短信一对多发送 原文链接 var request = require('request'); var crypto = require('crypto'); ...
- 短信平台API接口demo示例-Python/SMS/MultiSend
DEMO: SMS/MultiSend - 短信一对多发送 示例代码 原文链接 非加密代码示例 import requests import jsonappid = 'appid' # SUBMAIL ...
- 短信API接口demo示例-PHP/Message/XSend
DEMO:Message/XSend 原文链接 概览 加密签名计算方法请参考createSignature.php. 代码示列 <?php/****************** 非加密请求 示例 ...
- 什么是短信平台api接口?
自从人类诞生了手机,短信便随之而来,虽然后来有了更多的即时通信工具,如QQ和微信,个人用短信的机会已经很少了,但是对于企业公司来说,短信一直是非常方便的信息传递工具. 那些带有营销性质的企业和商家会通 ...
- 短信平台API接口调用-SUBMAIL
一.下载SDK开发包 前往文档中心 ->SDK 开发包下载页面,下载对应语言的SDK开发包. SUBMAIL 提供多种语言的开发包,SDK 集成了 SUBMAIL 的 API 请求方法,并将 A ...
- 验证码短信平台API接口的应用
事实上,现在不论是企业商家还是用户,都现已渐渐离不开短信验证码接口途径了.因为现在很多工作都在经过短信验证码接口实现各种短信服务功用,例如帐号的注册.身份的验证.付出时的提示.物流告诉等等.那么接下来 ...
最新文章
- WinForm窗体间如何传值
- 拖拽功能 php,基于Vue实现拖拽功能
- 准备好钱了吗?新款iPhone或于9月13日开始接受预订
- Linux 命令(27)—— echo 命令
- kali安装步骤失败 选择并安装软件_交通仿真建模软件Vissim7.0/6.0/5.3安装步骤
- 8uftp,8uftp使用教程图解
- Python Tox 使用笔记
- stm32 设置systick中断抢先式优先级
- 模拟信号和数字信号讲堂(一),模拟信号和数字信号之模拟信号详解
- python大战机器学习——人工神经网络
- 笔记本电源适配器的工作原理及其类型简介
- BEA广州UG腐败日--1st time
- 每个人都在使用LSTM,主流学术圈却只想让它的发明者闭嘴
- 李开复:渗透与价值——2012年的中国移动互联网
- java生成有理数_java-00设计-有理数类
- 智慧医疗信息系统HIS短信模板
- JS中this关键字详解
- 遂宁市社会组织专业社工参与乡村振兴示范项目“我为母亲留张影”主题活动
- 概念理解:外包、众包
- HTMLCSS相关面试题