jyy
2021-05-10 f12b519702e4c4c246238f79f95215f8cd26dea7
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
package com.matrix.system.shopXcx.api.tools;
 
import com.alibaba.fastjson.JSON;
import com.matrix.core.tools.LogUtil;
import com.matrix.system.shopXcx.api.tools.model.request.SmsSendRequest;
import com.matrix.system.shopXcx.api.tools.model.response.SmsSendResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
 
/**
 * @author jyy
 */
@Service("sMSTools")
public class SMSTools {
    /**
     * 用户平台API账号(非登录账号,示例:N1234567)
     */
    @Value("${sms.login_account}")
    private String loginAccount;
    /**
     * 用户平台API密码(非登录密码)
     */
    @Value("${sms.login_password}")
    private String loginPassword;
    /**
     * 用户平台请求URL
     */
    @Value("${sms.request_url}")
    private String requestUrl;
 
    /**
     * @param phone 手机号码
     * @param msg   短信内容
     * @return
     */
    public SmsSendResponse sendMsg(String phone, String msg) {
        //请求地址请登录253云通讯自助通平台查看或者询问您的商务负责人获取
        String smsSingleRequestServerUrl = requestUrl + "/msg/send/json";
        msg = "【新诚智慧】" + msg;
        //状态报告
        String report = "true";
        SmsSendRequest smsSingleRequest = new SmsSendRequest(loginAccount, loginPassword, msg, phone, report);
        String requestJson = JSON.toJSONString(smsSingleRequest);
        LogUtil.info("sendMsg requestParam: {}", requestJson);
        String response = sendSmsByPost(smsSingleRequestServerUrl, requestJson);
        SmsSendResponse smsSingleResponse = JSON.parseObject(response, SmsSendResponse.class);
        LogUtil.info("sendMsg response: {}", smsSingleResponse.toString());
        return smsSingleResponse;
    }
 
    public String sendSmsByPost(String path, String postContent) {
        URL url;
        try {
            url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            // 提交模式
            httpURLConnection.setRequestMethod("POST");
            //连接超时 单位毫秒
            httpURLConnection.setConnectTimeout(10000);
            //读取超时 单位毫秒
            httpURLConnection.setReadTimeout(10000);
 
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
//            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
//            printWriter.write(postContent);
//            printWriter.flush();
            httpURLConnection.connect();
            OutputStream os = httpURLConnection.getOutputStream();
            os.write(postContent.getBytes("UTF-8"));
            os.flush();
            StringBuilder sb = new StringBuilder();
            int httpRspCode = httpURLConnection.getResponseCode();
            if (httpRspCode == HttpURLConnection.HTTP_OK) {
                // 开始获取数据
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                br.close();
                return sb.toString();
            }
        } catch (Exception e) {
            LogUtil.error("发送请求失败", e);
            return null;
        }
        return null;
    }
 
}