代码描述:基于JAVA的群发短信接口调用示例

接口地址:http://api.yunzhixin.com:11140/txp/smsGroup


 starSky

     

 2018-02-09 10:04

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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

package com.dx.testspringmvc.test;

import com.fasterxml.jackson.databind.JsonNode;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;

import org.apache.commons.httpclient.NameValuePair;

import org.apache.commons.httpclient.methods.PostMethod;

import org.apache.commons.httpclient.params.HttpMethodParams;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

import java.security.MessageDigest;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

/**

* 群发短信发送示例代码

*

* @author www.yunzhixin.com

* @date 2018/02/09

*/

public class YunZhiXinChargeDemo {

/**

* 配置您申请的tradeKey

*/

private static final String TRADE_KEY = "6ccc12b375534660ab4c99c01c7dba9b";

/**

* 返回码字符

*/

private static final String RETURN_CODE = "return_code";

/**

* 返回成功时返回码的值

*/

private static final String SUCCESS = "0000";

/**

* 发送短信地址

*/

private static final String REQUEST_URL = ""http://api.yunzhixin.com:11140/txp/smsGroup";

public static void main(String[] args) {

//组装请求参数

Map<String, String> smsRequestParam = new HashMap<>(16);

//用户编号,注册www.yunzhixin.com的手机号码

smsRequestParam.put("account", "");

//需要发送的手机号,以英文逗号隔开

smsRequestParam.put("mobile", "");

//模板编号

smsRequestParam.put("tplId", "");

//短信所需传入的参数

smsRequestParam.put("params", "");

requestCharge(smsRequestParam);

}

/**

* 发送短信请求方法

*

* @param smsRequestParam 商户传入的参数集合

*/

private static void requestCharge(Map<String, String> smsRequestParam) {

//生成签名,组装参数

String signKey = getSignKey(smsRequestParam);

smsRequestParam.put("sign", signKey);

NameValuePair[] requestParams = combineRequestParam(smsRequestParam);

//发起短信发送请求

String response = postWithCharset(REQUEST_URL, requestParams, "UTF-8", 30 * 1000, 30 * 1000, true);

ObjectMapper objectMapper = new ObjectMapper();

try {

//解析返回json字符串,成功时打印订单号,失败时打印返回错误码

JsonNode jsonNode = objectMapper.readTree(response);

if (StringUtils.equals(jsonNode.get(RETURN_CODE).asText(), SUCCESS)) {

System.out.println("return_code:" + jsonNode.get(RETURN_CODE).asText());

System.out.println("totalNum:" + jsonNode.get("totalNum").asText());

return;

}

System.out.println("return_code:" + jsonNode.get(RETURN_CODE).asText());

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 生成签名

*

* @param requestParams 请求参数

* @return

*/

private static String getSignKey(Map<String, String> requestParams) {

/**

* account|tplId#tradeKey,加密字符串规则

* tradeKey 是由您申请的交易密钥

*/

StringBuffer source = new StringBuffer(50).append(requestParams.get("account")).append("|").append(requestParams.get("tplId")).append("#").append(TRADE_KEY);

return getMd5(source.toString(), true, "UTF-8").toUpperCase();

}

/**

* 组装请求参数

*

* @param requestParams 请求参数

* @return

*/

private static NameValuePair[] combineRequestParam(Map<String, String> requestParams) {

List<NameValuePair> nameValuePairs = new ArrayList<>();

requestParams.forEach((key, value) -> nameValuePairs.add(new NameValuePair(key, value)));

return nameValuePairs.toArray(new NameValuePair[nameValuePairs.size()]);

}

/**

* 发送Http请求工具

*

* @param requestUrl 请求地址

* @param requestParams 请求数据

* @param charset 编码格式

* @param connectionTimeout 连接超时时间(毫秒)

* @param soTimeout 读取超时时间(毫秒)

* @param fag 是否启用多线程

* @return

*/

private static String postWithCharset(String requestUrl, NameValuePair[] requestParams, String charset, int connectionTimeout, int soTimeout, boolean fag) {

String returnStr = "";

PostMethod postMethod = null;

try {

HttpClient client;

if (fag) {

MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();

client = new HttpClient(connectionManager);

} else {

client = new HttpClient();

}

postMethod = new PostMethod(requestUrl);

postMethod.setRequestBody(requestParams);

client.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);

postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset);

client.getHttpConnectionManager().getParams().setSoTimeout(soTimeout);

client.executeMethod(postMethod);

returnStr = postMethod.getResponseBodyAsString();

} catch (Exception e) {

e.printStackTrace();

} finally {

postMethod.releaseConnection();

}

return returnStr;

}

/**

* *

* MD5工具类

*

* @param plainText 需要加密的字符串

* @param md5Format true 32位,false 16 位

* @param charset 字符集格式

* @return

*/

private static String getMd5(String plainText, boolean md5Format, String charset) {

String md5Str = "";

try {

MessageDigest messageDigest = MessageDigest.getInstance("MD5");

messageDigest.update(plainText.trim().getBytes(charset));

byte[] hashBytes = messageDigest.digest();

int i;

StringBuffer tempStr = new StringBuffer(32);

for (int offset = 0; offset < hashBytes.length; offset++) {

i = hashBytes[offset];

if (i < 0) {

i += 256;

}

if (i < 16) {

tempStr.append("0");

}

tempStr.append(Integer.toHexString(i));

}

if (md5Format) {

md5Str = tempStr.toString().toUpperCase();

} else {

md5Str = tempStr.toString().substring(8, 24).toUpperCase();

}

} catch (Exception e) {

e.printStackTrace();

}

return md5Str;

}

}