ZQN
2024-06-19 e59e6a8a2fcf3ac2194a69927cd5690453a83e91
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
package com.project.common.sms;
 
import com.project.common.constant.Constants;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 短信http接口的java代码调用示例
 * 基于Apache HttpClient 4.3
 * @author Mr.Zhao
 */
 
public class YPSmsApi {
 
    /**
     * 请求地址
     */
    private static final String YP_SMS_URI = "http://yunpian.com/v1/sms/send.json";
 
    /**
     * KEY
     */
    private static final String API_KEY = "faf531146ca1e38abacd3862fb3fc32b";
 
    /**
     * 签名
     */
    private static final String SIGN = "【金明源】";
 
    /**
     * 验证码模板
     */
    public static final String VERIFY_CODE_TEMPLATE = "您的验证码是{}";
 
    /**
     * 审批通知模板
     */
    public static final String CHECK_NOTICE_TEMPLATE = SIGN + "{}提交了执法申请单,请您及时审批!";
 
    /**
     * 审批通过模板
     */
    public static final String CHECK_PASS_TEMPLATE = SIGN + "您提交的执法申请单已审批通过,请您及时查看!";
 
 
 
 
    /**
     * 发送短信
     * @param mobile  接受的手机号
     * @param msg    短信内容
     */
    public static String sendSms(String mobile, String msg)
    {
        Map<String, String> params = new HashMap<>(3);
        params.put("apikey", API_KEY);
        params.put("text", msg);
        params.put("mobile", mobile);
        return post(YP_SMS_URI, params);
    }
 
    public static void main(String[] args) {
        sendSms("18537821663", "【金明源】您的验证码是1234");
    }
 
 
    /**
     * 基于HttpClient 4.3的通用POST方法
     *
     * @param url        提交的URL
     * @param paramsMap  提交<参数,值>Map
     * @return  提交响应
     */
    public static String post(String url, Map<String, String> paramsMap)
    {
        CloseableHttpClient client = HttpClients.createDefault();
        String responseText = "";
        CloseableHttpResponse response = null;
        try {
            HttpPost method = new HttpPost(url);
            if (paramsMap != null) {
                List<NameValuePair> paramList = new ArrayList<NameValuePair>();
                for (Map.Entry<String, String> param : paramsMap.entrySet()) {
                    NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
                    paramList.add(pair);
                }
                method.setEntity(new UrlEncodedFormEntity(paramList, Constants.UTF8));
            }
            response = client.execute(method);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                responseText = EntityUtils.toString(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println(responseText);//此处打印在console后,会给出一个IP地址
        return responseText;
    }
}