SmsLeyunUtils.java
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.thinkgem.jeesite.common.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 乐云短信
*/
public class SmsLeyunUtils {
private final static Logger logger = LoggerFactory.getLogger(SmsLeyunUtils.class);
private final static String url = "http://lehuo520.cn/a/sms/api"; //账号的平台url地址
private final static String data = "username=jeesite&password=jeesite.com"; //账号的平台账号密码
public static void main(String[] args) {
String phone = "18500000000"; //收短信人手机号码;例如:18500000000 支持多号码,号码之间用英文逗号隔开,最多100个
String content = "【JeeSite】您好,您的验证码是:123456(请勿透露给其他人)感谢您的使用。"; //输入需要发送内容;例如:你好这是一条测试短信
String smsid = ""; //短信id,查询短信状态报告时需要,可为空
String sendTime = ""; //发送时间,为空立即发送
System.out.println(send(content, phone, sendTime)); //发短信
System.out.println(status(smsid, phone)); //取状态
System.out.println(reply()); //取上行 回复短信
}
/**
* 发送短信
* @param content,phone,sendtime
* @return {"result":"0","describing":"提交成功","sms":[{"phone":"18073110001,18073110002","smsid":"83bd18f1d48b4cc9b9fe7810c768ac43","status":"3"}]}
*/
public static String send(String content, String phone, String sendTime) {
String res = "";
try {
String param = data + "&phone=" + phone + "&content=" + URLEncoder.encode(content, "UTF-8")
+ "&sendTime=" + sendTime;
res = connectURL(url + "/send", param);
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
return res;
}
/**
* 获取状态
* @param smsid,phone可为空,为空取最近两天未获取状态报告,沦询间隔时间不能低于5分钟
* @return 请求错误返回页面示例: {"result":"-1","describing":"帐号不存在,请检查用户名或者密码是否正确","sms":[]} 请求成功返回页面示例:
* {"result":"0","describing":"提交成功","sms":[{"phone":"18073110001","smsid":"83bd18f1d48b4cc9b9fe7810c768ac43","status":"7"},{"phone":"18073110001","smsid":"83bd18f1d48b4cc9b9fe7810c768ac43","status":"8"}]}
*/
public static String status(String smsid, String phone) {
String res = "";
try {
String param = data + "&smsid=" + smsid;
res = connectURL(url + "/status", param);
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
return res;
}
/**
* 获取回复
* @param smsid:下发短信对应短信ID,taskId同一批任务ID
* @return {"result":"0","sms":[{"phone":"18073110001","neirong":"已收到","taskId":"83bd18f1d48b4cc9b9fe7810c768ac43"},"smsId":"83bd18f1d48b48j9b9fe7810c768ac43"}]}
*/
public static String reply() {
String res = "";
try {
String param = data;
res = connectURL(url + "/query", param);
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
return res;
}
/**
* 进行http提交
* @param
* @return
* @throws IOException
* @throws Exception
*/
private static String connectURL(String url, String param) throws IOException {
String res = "";
long startt = System.currentTimeMillis();
HttpURLConnection urlConn = null;
URL url1 = new URL(url);
urlConn = (HttpURLConnection) url1.openConnection();
urlConn.setRequestMethod("POST");
urlConn.setDoOutput(true);
OutputStream out = null;
BufferedReader rd = null;
try{
out = urlConn.getOutputStream();
out.write(param.getBytes("UTF-8"));
out.flush();
rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), "UTF-8"));
StringBuffer sb = new StringBuffer();
int ch;
while ((ch = rd.read()) > -1) {
sb.append((char) ch);
}
res = sb.toString().trim();
}finally {
if (out!=null){
out.close();
}
if (rd!=null){
rd.close();
}
}
long end = System.currentTimeMillis();
logger.debug("耗时:" + (end - startt));
return res;
}
}