Java对接彩信群发API,完整指南
2024-01-24 12:01
使用java对接彩信群发接口,是java开发者必备的技能。这里有一份完整的接入指南,希望对于java开发者有帮助。

一、准备工作
1、注册并登陆彩信群发平台

2、上传企业营业执照副本,完成企业认证
3、账户充值(新注册的客户,平台有10条免费的试用额度,请合理利用)
4、获取接口的appId和appKey。
二、开始接入
彩信的发送分为两步,第一步要创建彩信模版,第二步调用发送接口
第一步:创建彩信模版接口
彩信模版内容格式定义
1、彩信支持图片、视频、音频和文字,图片编码格式为base64,文字编码格式为gbk编码后转base64。
2、彩信最多支持15帧,帧与帧之间用英文分号(;)分隔,彩信总大小不能超过80KB。
3、彩信每帧需包含帧播放时间、文件类型、文件内容,文件内容可以为图片(支持jpg/gif格式)、视频(支持mp4格式)、音频(支持mp3格式)、文字(文字用txt格式存储,编码格式为gbk)。
4、同一帧最多允许一段文字和一种媒体文件(即同一帧中只允许展现视频、图片、音频中的一种媒体文件),多个媒体文件请分别放置在多帧中。
5、彩信完整数据报文格式:播放时间,文件类型|文件内容,文件类型|文件内容;播放时间,文件类型|文件内容,文件类型|文件内容;
代码示例
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
/**
* 彩信创建
* @author Veesing
*
*/
public class MmsTest {
final static String CREATE_URL = "https://vip.veesing.com/mmsApi/create";
final static String APPID = "";
final static String APPKEY = "";
public static void create() {
try {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(CREATE_URL);
post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
post.setRequestHeader("Connection", "close");
NameValuePair[] data = {
new NameValuePair("appId", APPID),
new NameValuePair("appKey", APPKEY),
new NameValuePair("title", "彩信标题"),
new NameValuePair("content", content())
};
post.setRequestBody(data);
client.executeMethod(post);
String result = new String(post.getResponseBodyAsString().getBytes());
System.out.println(result);
post.releaseConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 一帧<br>
* 多帧循环生成
*
* @return
*/
public static String content() {
StringBuffer sb = new StringBuffer();
// --------文件-----
sb.append(",jpg|" + encodeFile("文件路径"));
// --------文字-----
sb.append(",txt|" + encodeTxt("文字内容"));
sb.append(";");
return sb.substring(0, sb.length() - 1);
}
public static String encodeFile(String filePath) {
byte[] data = null;
try {
InputStream in = new FileInputStream(filePath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return Base64.getEncoder().encodeToString(data).replaceAll("[\\t\\n\\r]", "");
}
public static String encodeTxt(String txt) {
try {
return Base64.getEncoder().encodeToString(new String(txt).getBytes("gbk"));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}第二步:彩信发送接口
代码示例
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
/**
* 彩信发送
* @author Veesing
*
*/
public class MmsTest {
final static String SEND_URL = "https://vip.veesing.com/mmsApi/send";
final static String APPID = "";
final static String APPKEY = "";
public static void send() {
try {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(SEND_URL);
post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
post.setRequestHeader("Connection", "close");
NameValuePair[] data = {
new NameValuePair("appId", APPID),
new NameValuePair("appKey", APPKEY),
new NameValuePair("mmsId", "xxxxxx"),//第一步创建的id
// new NameValuePair("sendTime", "2020-11-07 14:37:31"),
new NameValuePair("phone", "131xxxx0000,1320000xxxx") };//多个手机号用,号隔开
post.setRequestBody(data);
client.executeMethod(post);
String result = new String(post.getResponseBodyAsString().getBytes());
System.out.println(result);
post.releaseConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
}以上就是用java对接彩信群发接口指南的全部啦,快来试试吧~