1821349743@qq.com
2023-02-20 fca33f5fba0959d665d749cc78730c1efd1159e8
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
package com.product.text.message.util;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
 
import org.apache.commons.codec.binary.Base64;
 
import com.alibaba.fastjson.JSON;
import com.product.core.entity.FieldSetEntity;
import com.product.text.message.config.CmnConst;
import com.product.text.message.entity.SendReq;
import com.product.text.message.entity.SendRes;
 
/**
 *     云MAS服务Http发送
* Copyright  PRODUCT-BASE
* @Title: PRODUCT-BASE-
* @Project: HttpSmsSendUtil
* @Date: 2021年6月25日 下午5:09:59
* @Author: 杜洪波
* @Description:
 */
public class HttpSmsSendUtil {
 
    private static String apId = "zgctjt";   //用户
    private static String secretKey = "zgctjt2019";  //密码
    private static String ecName = "自贡市城市建设投资开发集团有限公司"; // 集团名称
    private static String sign = "9OOWx4mtn"; // 网关签名编码
    private static String addSerial = ""; // 拓展码 填空
    public static String url = "http://112.35.1.155:1992/sms/norsubmit";// 请求url
 
    public static void sendMsg(FieldSetEntity fseMessageInfo)  {
        try {
            SendReq sendReq = new SendReq();
            sendReq.setApId(fseMessageInfo.getString(CmnConst.USER_ACCOUNT));
            sendReq.setEcName(fseMessageInfo.getString(CmnConst.ECNAME));
            sendReq.setSecretKey(fseMessageInfo.getString(CmnConst.SECRETKEY));
            sendReq.setContent(fseMessageInfo.getString("content"));
            sendReq.setMobiles(fseMessageInfo.getString(CmnConst.RECEIVE_USER));
            sendReq.setAddSerial(fseMessageInfo.getString(CmnConst.ADDSERIAL));
            sendReq.setSign(fseMessageInfo.getString(CmnConst.SIGN));
    
            StringBuilder stringBuffer = new StringBuilder();
            stringBuffer.append(sendReq.getEcName());
            stringBuffer.append(sendReq.getApId());
            stringBuffer.append(sendReq.getSecretKey());
            stringBuffer.append(sendReq.getMobiles());
            stringBuffer.append(sendReq.getContent());
            stringBuffer.append(sendReq.getSign());
            stringBuffer.append(sendReq.getAddSerial());
    
            sendReq.setMac(Md5Util.MD5(stringBuffer.toString()).toLowerCase());
    
            String reqText = JSON.toJSONString(sendReq);
    
            String encode = Base64.encodeBase64String(reqText.getBytes("UTF-8"));
    
            String resStr = sendPost(url, encode);
    
            SendRes sendRes = JSON.parseObject(resStr, SendRes.class);
    
            if (sendRes.isSuccess()) {
                fseMessageInfo.setValue(CmnConst.SEND_STAT, 0);
            } else {
                fseMessageInfo.setValue(CmnConst.SEND_STAT, 1);
                fseMessageInfo.setValue(CmnConst.FAILURE_REASON, sendRes.getMsgGroup());
            }
        } catch (IOException e) {
            fseMessageInfo.setValue(CmnConst.SEND_STAT, 1);
            fseMessageInfo.setValue(CmnConst.FAILURE_REASON, e.getMessage());
        }
    }
    
    /**
     * 多用户发送短信信息
     * 
     * @param mobiles 手机号码逗号分隔
     * @param content 短信内容
     * @return 返回1表示成功,0表示失败
     * @throws IOException
     */
    public static int sendMsg(String mobiles, String content) throws IOException {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowDatestr = sdf.format(calendar.getTimeInMillis());  //发送日期
        content += nowDatestr; // 短信内容后跟个日期时间(可有可无),需求要求
 
        SendReq sendReq = new SendReq();
        sendReq.setApId(apId);
        sendReq.setEcName(ecName);
        sendReq.setSecretKey(secretKey);
        sendReq.setContent(content);
        sendReq.setMobiles(mobiles);
        sendReq.setAddSerial(addSerial);
        sendReq.setSign(sign);
 
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(sendReq.getEcName());
        stringBuffer.append(sendReq.getApId());
        stringBuffer.append(sendReq.getSecretKey());
        stringBuffer.append(sendReq.getMobiles());
        stringBuffer.append(sendReq.getContent());
        stringBuffer.append(sendReq.getSign());
        stringBuffer.append(sendReq.getAddSerial());
 
        sendReq.setMac(Md5Util.MD5(stringBuffer.toString()).toLowerCase());
 
        String reqText = JSON.toJSONString(sendReq);
 
        String encode = Base64.encodeBase64String(reqText.getBytes("UTF-8"));
 
        String resStr = sendPost(url, encode);
 
        SendRes sendRes = JSON.parseObject(resStr, SendRes.class);
 
        if (sendRes.isSuccess() && !"".equals(sendRes.getMsgGroup()) && "success".equals(sendRes.getRspcod())) {
            return 1;
        } else {
            return 0;
        }
    }
 
    // main方法测试发送短信,返回1表示成功,0表示失败
    public static void main(String[] args) throws IOException {
        String msg = "这是发送短信的内容!";
        int result = sendMsg("15182792202", msg);
        System.out.println("===" + result);
        if (result!=1) {
            System.out.println("生成系统日志");
        }else {
            System.out.println("发送成功!");
        }
    }
 
    /**
     * 向指定 URL 发送POST方法的请求
     *
     * @param url   发送请求的 URL
     * @param param 请求参数
     * @return 所代表远程资源的响应结果
     */
    private static String sendPost(String url, String param) {
        OutputStreamWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("contentType", "utf-8");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            conn.setDoOutput(true);
            conn.setDoInput(true);
 
            out = new OutputStreamWriter(conn.getOutputStream());
            out.write(param);
            out.flush();
 
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += "\n" + line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }
 
}