代码描述:基于JAVA的上行回复接口调用示例

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


 starSky

     

 2018-02-27 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

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 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 PullReplyDemo {

/**

* 配置您申请的tradeKey

*/

private static final String TRADE_KEY = "";

/**

* 返回码字符

*/

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/pullReply";

public static void main(String[] args) {

////组装请求参数

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

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

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

requestCharge(smsRequestParam);

}

/**

* 上行回复请求方法

*

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

*/

private static void submitTemplate(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("status:" + jsonNode.get("status").asText());

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

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

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

}

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|tpl_id#tradeKey,加密字符串规则

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

*/

StringBuffer source = new StringBuffer(50).append(requestParams.get("account")).append("|").append(requestParams.get("tpl_id")).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;

}

}