puzhibing
2023-11-28 7b726d5ff439e7b8bb66369ecc5881e370286bc8
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
package com.stylefeng.guns.modular.system.util;
 
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
 
 
@Component
public class SmsUtil {
 
    private static Logger logger = LoggerFactory.getLogger(SmsUtil.class);
 
    public static void main(String[] args) {
        //短信下发
        String sendUrl = "https://xxx/msg/send/json";
        Map map = new HashMap();
        map.put("account","N*******");//API账号
        map.put("password","************");//API密码
        map.put("msg","您本次验证码为:{0}。请勿泄露他人,如非本人操作可勿略本短信");//短信内容
        map.put("phone","15300000000");//手机号
        map.put("report","true");//是否需要状态报告
        map.put("extend","123");//自定义扩展码
        JSONObject js = (JSONObject) JSONObject.toJSON(map);
        logger.debug(sendSmsByPost(sendUrl,js.toString()));
        //查询余额
        String balanceUrl = "https://xxx/msg/balance/json";
        Map map1 = new HashMap();
        map1.put("account","N*******");
        map1.put("password","************");
        JSONObject js1 = (JSONObject) JSONObject.toJSON(map1);
        logger.debug(sendSmsByPost(balanceUrl,js1.toString()));
    }
 
 
    public static String sendSmsByPost(String path, String postContent) {
        URL url = null;
        try {
            url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            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) {
            e.printStackTrace();
        }
        return null;
    }
}