RailwayThirdTools.java
4.81 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
package com.xiniunet.service.railway.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiniunet.framework.base.BaseRequest;
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.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.mortbay.util.ajax.JSON;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.*;
public class RailwayThirdTools {
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 {
/* Map<String,String> params = ObjectToMap(request);*/
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/json");
httpPost.addHeader("User-Agent", "imgfornote");
// 设置请求的参数
/* 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 StringEntity( com.alibaba.fastjson.JSON.toJSONString(request)));
// 执行请求
HttpResponse response = httpClient.execute(httpPost);
JSONObject jsonObject = (JSONObject) JSONObject.parse(EntityUtils.toString(response.getEntity(), "utf-8"));
return jsonObject;
}
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;
}
}