1
zj
2025-06-23 dc9bd22833255bc602dd42c7f603ecb50842ab35
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package smsmoduyun.v1.yun;
 
import com.alibaba.fastjson.JSONObject;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.util.ArrayList;
 
 
public class SmsSingleSender {
    String accesskey;
    String secretkey;
    // 同时支持http和https两种协议,具体根据自己实际情况使用。
    String url = "https://live.mordula.com/sms/v1/sendsinglesms";
    private static int TIMEOUT = 30 * 1000;
 
    SmsSenderUtil util = new SmsSenderUtil();
 
    public SmsSingleSender(String accesskey, String secretkey) throws Exception {
        this.accesskey = accesskey;
        this.secretkey = secretkey;
    }
 
    /**
     * 普通单发短信接口,明确指定内容,如果有多个签名,请在内容中以【】的方式添加到信息内容中,否则系统将使用默认签名
     * 
     * @param type        短信类型,0 为普通短信,1 营销短信
     * @param nationCode  国家码,如 86 为中国
     * @param phoneNumber 不带国家码的手机号
     * @param msg         信息内容,必须与申请的模板格式一致,否则将返回错误
     * @param extend      扩展码,可填空
     * @param ext         服务端原样返回的参数,可填空
     * @return {@link}SmsSingleSenderResult
     * @throws Exception
     */
    public SmsSingleSenderResult send(int type, String nationCode, String phoneNumber, String msg, String extend,
            String ext) throws Exception {
        /*
         * 请求包体 { "tel": { "nationcode": "86", "mobile": "13788888888" }, "type": 0,
         * "msg": "你的验证码是1234", "sig": "fdba654e05bc0d15796713a1a1a2318c", "time":
         * 1479888540, "extend": "", "ext": "" } 应答包体 { "result": 0, "errmsg": "OK",
         * "ext": "", "sid": "xxxxxxx", "fee": 1 }
         */
        // 校验 type 类型
        if (0 != type && 1 != type) {
            throw new Exception("type " + type + " error");
        }
        if (null == extend) {
            extend = "";
        }
        if (null == ext) {
            ext = "";
        }
 
        // 按照协议组织 post 请求包体
        long random = util.getRandom();
        long curTime = System.currentTimeMillis() / 1000;
 
        JSONObject data = new JSONObject();
 
        JSONObject tel = new JSONObject();
        tel.put("nationcode", nationCode);
        tel.put("mobile", phoneNumber);
 
        data.put("type", type);
        data.put("msg", msg);
        data.put("sig", util.strToHash(
                String.format("secretkey=%s&random=%d&time=%d&mobile=%s", secretkey, random, curTime, phoneNumber)));
        data.put("tel", tel);
        data.put("time", curTime);
        data.put("extend", extend);
        data.put("ext", ext);
 
        // 与上面的 random 必须一致
        String wholeUrl = String.format("%s?accesskey=%s&random=%d", url, accesskey, random);
        HttpURLConnection conn = util.getPostHttpConn(wholeUrl);
 
        conn.setConnectTimeout(TIMEOUT);
        conn.setReadTimeout(TIMEOUT);
 
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
 
        wr.write(data.toString());
        wr.flush();
        System.out.println(wholeUrl);
        System.out.println(data.toString());
 
        // 显示 POST 请求返回的内容
        StringBuilder sb = new StringBuilder();
        int httpRspCode = conn.getResponseCode();
        SmsSingleSenderResult result;
        if (httpRspCode == HttpURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            br.close();
            JSONObject json =  JSONObject.parseObject(sb.toString());
            result = util.jsonToSmsSingleSenderResult(json);
        } else {
            result = new SmsSingleSenderResult();
            result.result = httpRspCode;
            result.errMsg = "http error " + httpRspCode + " " + conn.getResponseMessage();
        }
 
        return result;
    }
 
    /**
     * 指定模板单发
     * 
     * @param nationCode  国家码,如 86 为中国
     * @param phoneNumber 不带国家码的手机号
     * @param templId     信息内容
     * @param params      模板参数列表,如模板 {1}...{2}...{3},那么需要带三个参数
     * @param sign        签名,如果填空,系统会使用默认签名
     * @param extend      扩展码,可填空
     * @param ext         服务端原样返回的参数,可填空
     * @return {@link}SmsSingleSenderResult
     * @throws Exception
     */
    public SmsSingleSenderResult sendWithParam(String nationCode, String phoneNumber, int templId,
            ArrayList<String> params, String sign, String extend, String ext) throws Exception {
        /*
         * 请求包体 { "tel": { "nationcode": "86", "mobile": "13788888888" }, "sign":
         * "Kewail云", "tpl_id": 19, "params": [ "验证码", "1234", "4" ], "sig":
         * "fdba654e05bc0d15796713a1a1a2318c", "time": 1479888540, "extend": "", "ext":
         * "" } 应答包体 { "result": 0, "errmsg": "OK", "ext": "", "sid": "xxxxxxx", "fee":
         * 1 }
         */
        if (null == nationCode || 0 == nationCode.length()) {
            nationCode = "86";
        }
        if (null == params) {
            params = new ArrayList<>();
        }
        if (null == sign) {
            sign = "";
        }
        if (null == extend) {
            extend = "";
        }
        if (null == ext) {
            ext = "";
        }
 
        long random = util.getRandom();
        long curTime = System.currentTimeMillis() / 1000;
 
        JSONObject data = new JSONObject();
 
        JSONObject tel = new JSONObject();
        tel.put("nationcode", nationCode);
        tel.put("mobile", phoneNumber);
 
        data.put("tel", tel);
        data.put("sig", util.calculateSigForTempl(secretkey, random, curTime, phoneNumber));
        data.put("tpl_id", templId);
        data.put("params", util.smsParamsToJSONArray(params));
        data.put("sign", sign);
        data.put("time", curTime);
        data.put("extend", extend);
        data.put("ext", ext);
 
        String wholeUrl = String.format("%s?accesskey=%d&random=%d", url, accesskey, random);
        HttpURLConnection conn = util.getPostHttpConn(wholeUrl);
        conn.setConnectTimeout(TIMEOUT);
        conn.setReadTimeout(TIMEOUT);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
        wr.write(data.toString());
        wr.flush();
 
        // 显示 POST 请求返回的内容
        StringBuilder sb = new StringBuilder();
        int httpRspCode = conn.getResponseCode();
        SmsSingleSenderResult result;
        if (httpRspCode == HttpURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            br.close();
            JSONObject json = JSONObject.parseObject(sb.toString());
            result = util.jsonToSmsSingleSenderResult(json);
        } else {
            result = new SmsSingleSenderResult();
            result.result = httpRspCode;
            result.errMsg = "http error " + httpRspCode + " " + conn.getResponseMessage();
        }
 
        return result;
    }
 
    public static void main(String[] args) throws Exception {
        SmsSenderUtil util = new SmsSenderUtil();
        HttpURLConnection conn = util.getPostHttpConn("https://www.baidu.com");
 
        conn.setConnectTimeout(TIMEOUT);
        conn.setReadTimeout(TIMEOUT);
 
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
        wr.write("");
        wr.flush();
    }
}