RailwayThirdTools.java
5.25 KB
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
package com.xiniunet.service.railway.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiniunet.framework.base.BaseRequest;
import com.xiniunet.framework.cache.CacheManager;
import com.xiniunet.framework.constant.CacheTimeEnum;
import com.xiniunet.framework.util.EncryptUtil;
import com.xiniunet.framework.util.SpringContext;
import com.xiniunet.service.railway.Constant;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.*;
public class RailwayThirdTools {
private static Logger logger = LoggerFactory.getLogger(RailwayThirdTools.class);
private static CacheManager cacheManager = SpringContext.getApplicationContext().getBean(CacheManager.class);
public static Map<?, ?> objectToMap(Object obj) {
if(obj == null)
return null;
return new org.apache.commons.beanutils.BeanMap(obj);
}
public static void main(String[] args) throws Exception{
String url = "https://api.netease.im/nimserver/team/create.action";
JSONArray members = new JSONArray();
members.add(0,"sqqtalk");
Map<String,String> params = new HashMap<String,String>();
params.put("tname","teamsqq2");
params.put("owner","sqqtalk2");
params.put("members",members.toJSONString());
params.put("msg","welcome to xntalk team");
params.put("magree","1");
params.put("joinmode","0");
// 执行请求
JSONObject jsonObject = railwayThird(url,params);
// 打印执行结果
System.out.println(jsonObject.toJSONString());
}
public static JSONObject railwayThird(String url, BaseRequest request) throws IOException {
String key = EncryptUtil.MD5(url + JSON.toJSONString(request));
Object value = cacheManager.get(key);
if(value != null) {
logger.warn("缓存命中");
return JSON.parseObject(value.toString());
}
if(!url.equals(Constant.RALWAY_THIRD_SAFETY_ESCORT_POSITION_URL)) {
logger.warn("缓存未命中," + url + " " + JSON.toJSONString(request));
}
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
// 设置请求的header
httpPost.addHeader("Content-Type", "application/json");
httpPost.setEntity(new StringEntity( com.alibaba.fastjson.JSON.toJSONString(request)));
// 执行请求
HttpResponse response = httpClient.execute(httpPost);
JSONObject result = (JSONObject) JSONObject.parse(EntityUtils.toString(response.getEntity(), "utf-8"));
cacheManager.set(key, result.toJSONString(), CacheTimeEnum.ONE_MINUTE);
return result;
}
public static Map ObjectToMap(Object obj){
Map<String,Object> resultMap = new HashMap<String,Object>();
Method[] methods = obj.getClass().getDeclaredMethods();
for (Method method : methods) {
try {
if (method.getName().startsWith("get")) {
String field = method.getName();
field = field.substring(field.indexOf("get") + 3);
field = field.toLowerCase().charAt(0) + field.substring(1); // 转换成小写
Object value = method.invoke(obj, (Object[]) null);
if (value != null) {
resultMap.put(field, value.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return resultMap;
}
public static JSONObject railwayThird(String url,Map<String,String> params) throws IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
String nonce = System.currentTimeMillis() + "" + new Random(System.currentTimeMillis()).nextInt(10000);
String curTime = String.valueOf((new Date()).getTime() / 1000L);
// 设置请求的header
httpPost.addHeader("Nonce", nonce);
httpPost.addHeader("CurTime", curTime);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
// 设置请求的参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
// 执行请求
HttpResponse response = httpClient.execute(httpPost);
JSONObject jsonObject = (JSONObject) JSONObject.parse(EntityUtils.toString(response.getEntity(), "utf-8"));
return jsonObject;
}
}