/
zj
2025-05-02 9102aa7e0b42ce5b9667fa3b67fede889df60fc0
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
package smsmoduyun.v2.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.List;
 
 
 
public class SmsSingleSender {
//    String accesskey;
//    String secretkey;
    // 同时支持http和https两种协议,具体根据自己实际情况使用。
    String url = "https://live.mordula.com/sms/v2/sendsinglesms";
    // String url =
    // "http://2p8773516g.qicp.vip:16416/live.kewail.com/sms/v2/sendsinglesms";
 
    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 signId      短信签名的Id
     * @param templateId  短信模板Id
     * @param params      ,短信的变量参数值 ,将短信模板中的变量{0},{1}替换为参数中的值,如果短信模板中没有变量,这这个值填null
     * @param ext         服务端原样返回的参数,可填空
     * @return {@link}SmsSingleSenderResult
     * @throws Exception
     */
    public SmsSingleSenderResult send(int type, String nationcode, String phoneNumber, String signId, String templateId,
            List<String> params, String ext, String accesskey, String secretkey) throws Exception {
 
        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("signId", signId);
        data.put("templateId", templateId);
        data.put("type", type);
        if (params != null && params.size() > 0) {
            data.put("params", util.smsParamsToJSONArray(params));
        }
 
        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("ext", ext);
 
        // 与上面的 random 必须一致
        String wholeUrl = String.format("%s?accesskey=%s&random=%d", url, accesskey, random);
        HttpURLConnection conn = util.getPostHttpConn(wholeUrl);
 
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
//        System.out.println(data.toString());
        wr.write(data.toString());
        wr.flush();
 
//        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();
//            System.out.println(sb.toString());
            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;
    }
 
}