puzhibing
2023-02-15 2811bab657aab4145b65a45a824fb63e93b58e30
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package com.stylefeng.guns.modular.system.controller.util;
 
 
import javax.net.ssl.*;
import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.*;
 
//如果JDK版本是1.8,可使用原生Base64类
 
//如果JDK版本低于1.8,请使用三方库提供Base64类
//import org.apache.commons.codec.binary.Base64;
 
public class SendSms {
 
    //无需修改,用于格式化鉴权头域,给"X-WSSE"参数赋值
    private static final String WSSE_HEADER_FORMAT = "UsernameToken Username=\"%s\",PasswordDigest=\"%s\",Nonce=\"%s\",Created=\"%s\"";
    //无需修改,用于格式化鉴权头域,给"Authorization"参数赋值
    private static final String AUTH_HEADER_VALUE = "WSSE realm=\"SDP\",profile=\"UsernameToken\",type=\"Appkey\"";
    //必填,请参考"开发准备"获取如下数据,替换为实际值
    private static String  url = "https://api.rtc.huaweicloud.com:10443/sms/batchSendSms/v1"; //APP接入地址+接口访问URI//https://api.rtc.huaweicloud.com:10443
    private static String appKey = "77lVYXy28011Ia6r43L6180kvdTv"; //APP_Key
    private static String appSecret = "8heBt1YBCL71AF1G9UkxZpoUFqIY"; //APP_Secret
    private static String sender1 = "881907307922"; //国内短信签名通道号或国际/港澳台短信通道号 (验证码类)
    private static String sender2 = "8819073039050"; //国内短信签名通道号或国际/港澳台短信通道号(通知类)
    private static String templateId1 = "6090edbdbd7e4550aae87bea35454d61"; //模板ID(验证码类)
    private static String templateId2 = "18134ace828144f4bec31fa326e4de86"; //模板ID(通知类)
    //条件必填,国内短信关注,当templateId指定的模板类型为通用模板时生效且必填,必须是已审核通过的,与模板类型一致的签名名称
    //国际/港澳台短信不用关注该参数
    private static String signature = "全客通"; //签名名称
 
    //必填,全局号码格式(包含国家码),示例:+8615123456789,多个号码之间用英文逗号分隔
    String receiver = "+8615123456789,+8615234567890"; //短信接收人号码
 
    //选填,短信状态报告接收地址,推荐使用域名,为空或者不填表示不接收状态报告
    private static String statusCallBack = "";
 
    /** 发送短信成功之后的回调信息
     * {"result":[{"originTo":"13981969442","createTime":"2019-08-01T01:13:21Z","from":"8819073039050","smsMsgId":"f69b2293-9285-4f48-933e-6bcba1ea5710_579546688","status":"000000"}],"code":"000000","description":"Success"}
     */
 
    /**
     * 选填,使用无变量模板时请赋空值 String templateParas = "";
     * 单变量模板示例:模板内容为"您的验证码是${NUM_6}"时,templateParas可填写为"[\"369751\"]"
     * 双变量模板示例:模板内容为"您有${NUM_2}件快递请到${TXT_32}领取"时,templateParas可填写为"[\"3\",\"人民公园正门\"]"
     * 查看更多模板格式规范:常见问题>业务规则>短信模板内容审核标准
     */
    String templateParas = "[\"369751\"]"; //模板变量
 
    public static String send (String receiver,String templateParas,Integer type) throws Exception {
 
        //请求Body,不携带签名名称时,signature请填null
 
        String body="";
 
        if(type==1){
            //验证码类
            body =buildRequestBody(sender1, receiver, templateId1,templateParas , statusCallBack, signature);
        }else if(type==2){
            //通知类
            body =buildRequestBody(sender2, receiver, templateId2,templateParas , statusCallBack, signature);
        }
 
        if (null == body || body.isEmpty()) {
            System.out.println("body is null.");
            return null;
        }
 
        //请求Headers中的X-WSSE参数值
        String wsseHeader = buildWsseHeader(appKey, appSecret);
        if (null == wsseHeader || wsseHeader.isEmpty()) {
            System.out.println("wsse header is null.");
            return null;
        }
 
        DataOutputStream out = null;
        BufferedReader in = null;
        StringBuffer result = new StringBuffer();
        HttpsURLConnection connection = null;
        InputStream is = null;
 
        //为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
        HostnameVerifier hv = new HostnameVerifier() {
 
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };
        trustAllHttpsCertificates();
 
        try {
            URL realUrl = new URL(url);
            connection = (HttpsURLConnection) realUrl.openConnection();
 
            connection.setHostnameVerifier(hv);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(true);
            //请求方法
            connection.setRequestMethod("POST");
            //请求Headers参数
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Authorization", AUTH_HEADER_VALUE);
            connection.setRequestProperty("X-WSSE", wsseHeader);
 
            connection.connect();
            out = new DataOutputStream(connection.getOutputStream());
            out.writeBytes(body); //发送请求Body参数
            out.flush();
            out.close();
 
            int status = connection.getResponseCode();
            if (200 == status) { //200
                is = connection.getInputStream();
            } else { //400/401
                is = connection.getErrorStream();
            }
            in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line = "";
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            System.out.println(result.toString()); //打印响应消息实体
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != out) {
                    out.close();
                }
                if (null != is) {
                    is.close();
                }
                if (null != in) {
                    in.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result.toString();
    }
 
    /**
     * 构造请求Body体
     * @param sender
     * @param receiver
     * @param templateId
     * @param templateParas
     * @param statusCallBack
     * @param signature | 签名名称,使用国内短信通用模板时填写
     * @return
     */
    static String buildRequestBody(String sender, String receiver, String templateId, String templateParas,
                                   String statusCallBack, String signature) {
        if (null == sender || null == receiver || null == templateId || sender.isEmpty() || receiver.isEmpty()
                || templateId.isEmpty()) {
            System.out.println("buildRequestBody(): sender, receiver or templateId is null.");
            return null;
        }
        Map<String, String> map = new HashMap<String, String>();
 
        map.put("from", sender);
        map.put("to", receiver);
        map.put("templateId", templateId);
        if (null != templateParas && !templateParas.isEmpty()) {
            map.put("templateParas", templateParas);
        }
        if (null != statusCallBack && !statusCallBack.isEmpty()) {
            map.put("statusCallback", statusCallBack);
        }
        if (null != signature && !signature.isEmpty()) {
            map.put("signature", signature);
        }
 
        StringBuilder sb = new StringBuilder();
        String temp = "";
 
        for (String s : map.keySet()) {
            try {
                temp = URLEncoder.encode(map.get(s), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            sb.append(s).append("=").append(temp).append("&");
        }
 
        return sb.deleteCharAt(sb.length()-1).toString();
    }
 
    /**
     * 构造X-WSSE参数值
     * @param appKey
     * @param appSecret
     * @return
     */
    static String buildWsseHeader(String appKey, String appSecret) {
        if (null == appKey || null == appSecret || appKey.isEmpty() || appSecret.isEmpty()) {
            System.out.println("buildWsseHeader(): appKey or appSecret is null.");
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        String time = sdf.format(new Date()); //Created
        String nonce = UUID.randomUUID().toString().replace("-", ""); //Nonce
 
        MessageDigest md;
        byte[] passwordDigest = null;
 
        try {
            md = MessageDigest.getInstance("SHA-256");
            md.update((nonce + time + appSecret).getBytes());
            passwordDigest = md.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
 
        //如果JDK版本是1.8,请加载原生Base64类,并使用如下代码
        String passwordDigestBase64Str = Base64.getEncoder().encodeToString(passwordDigest); //PasswordDigest
        //如果JDK版本低于1.8,请加载三方库提供Base64类,并使用如下代码
        //String passwordDigestBase64Str = Base64.encodeBase64String(passwordDigest); //PasswordDigest
        //若passwordDigestBase64Str中包含换行符,请执行如下代码进行修正
        //passwordDigestBase64Str = passwordDigestBase64Str.replaceAll("[\\s*\t\n\r]", "");
        return String.format(WSSE_HEADER_FORMAT, appKey, passwordDigestBase64Str, nonce, time);
    }
 
    /**
     * 忽略证书信任问题
     * @throws Exception
     */
    static void trustAllHttpsCertificates() throws Exception {
        TrustManager[] trustAllCerts = new TrustManager[] {
                new X509TrustManager() {
                    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                        return;
                    }
                    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                        return;
                    }
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
                }
        };
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    }
    /*测试发送短信*/
    public static void main(String[] args) throws Exception{
/*
        //String  res = send("13981969442","[\"123456\"]",1);//验证码测试
        JSONObject object = new JSONObject("{\"result\":[{\"originTo\":\"13981969442\",\"createTime\":\"2019-08-01T11:23:52Z\",\"from\":\"881907307922\",\"smsMsgId\":\"f9a397a3-8e36-479a-834c-a20a7a466d8c_606422718\",\"status\":\"000000\"}],\"code\":\"000000\",\"description\":\"Success\"}");
        String code=object.getString("code");
        //String  res1 = send("13981969442","[\"2017/07/16\",\"成都北\",\"14:30\",\"3\"]",2);//通知类测试  日期格式只能是2017/07/16这种的*/
    }
}