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
163
164
165
166
167
168
169
170
package mobvista.prd.datasource.util;
import com.google.gson.JsonObject;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class HttpUtil {
public static String postRequest(String reqUrl, Map<String, Object> paramMap, Logger log) throws Exception {
PostMethod post = new PostMethod(reqUrl);
HttpClient client = new HttpClient();
try {
if (paramMap!= null) {
Iterator<Entry<String, Object>> itr = paramMap.entrySet().iterator();
Entry<String, Object> entry;
while (itr.hasNext()) {
entry = itr.next();
post.addParameter(entry.getKey(), String.valueOf(entry.getValue()));
}
}
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
client.getHttpConnectionManager().getParams().setConnectionTimeout(50000);
int status = client.executeMethod(post);
if (status == HttpStatus.SC_OK) {
return post.getResponseBodyAsString();
} else {
log.error("=============http client error===============");
log.error("param=" + post.getParameters().toString());
log.error(post.getResponseBodyAsString());
log.error("=============================================");
throw new RuntimeException("HTTP请求失败");
}
} finally {
post.releaseConnection();
}
}
public static String getRequest(String reqUrl, Map<String, Object> paramMap, Logger log) throws Exception {
return getRequest(reqUrl, paramMap, null, log);
}
public static String getRequest(String reqUrl, Map<String, Object> paramMap, Map<String, String> headerMap, Logger log) throws Exception {
GetMethod get = new GetMethod(reqUrl);
HttpClient client = new HttpClient();
StringBuilder builder = new StringBuilder();
try {
if (paramMap!= null) {
Iterator<Entry<String, Object>> itr = paramMap.entrySet().iterator();
Entry<String, Object> entry;
while (itr.hasNext()) {
entry = itr.next();
if (builder.length() == 0) {
builder.append("?").append(entry.getKey()).append("=").append(entry.getValue());
} else {
builder.append("&").append(entry.getKey()).append("=").append(entry.getValue());
}
}
}
if (headerMap != null) {
Entry<String, String> entry = null;
Iterator<Entry<String, String>> itr = headerMap.entrySet().iterator();
List<Header> headers = new ArrayList<Header>();
while (itr.hasNext()) {
entry = itr.next();
headers.add(new Header(entry.getKey(), entry.getValue()));
}
client.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
}
get = new GetMethod(URLDecoder.decode(reqUrl + builder.toString(), "UTF-8"));
get.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
client.getHttpConnectionManager().getParams().setConnectionTimeout(50000);
int status = client.executeMethod(get);
if (status == HttpStatus.SC_OK) {
return get.getResponseBodyAsString();
} else {
log.error("=============http client error===============");
log.error("param=" + paramMap);
log.error(get.getResponseBodyAsString());
log.error("=============================================");
throw new RuntimeException("HTTP请求失败 \n " + get.getResponseBodyAsString());
}
} finally {
if (get != null) {
get.releaseConnection();
}
}
}
/**
*
* @param url
* @param json
* @param log
* @return
* @throws Exception
*/
public static String postRequestJson (String url, JsonObject json, Logger log) throws Exception {
return postRequestJson(url, json, null, log);
}
/**
*
* @param url
* @param json
* @param headerMap
* @param log
* @return
* @throws Exception
*/
public static String postRequestJson (String url, JsonObject json, Map<String, String> headerMap, Logger log) throws Exception {
PostMethod post = new PostMethod(url);
HttpClient client = new HttpClient();
try {
if (headerMap != null) {
Entry<String, String> entry = null;
Iterator<Entry<String, String>> itr = headerMap.entrySet().iterator();
List<Header> headers = new ArrayList<Header>();
while (itr.hasNext()) {
entry = itr.next();
headers.add(new Header(entry.getKey(), entry.getValue()));
}
client.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
}
RequestEntity requestEntity = new StringRequestEntity(json.toString(), "application/json", "UTF-8");
post.setRequestEntity(requestEntity);
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
client.getHttpConnectionManager().getParams().setConnectionTimeout(50000);
int status = client.executeMethod(post);
if (status == HttpStatus.SC_OK) {
return post.getResponseBodyAsString();
} else {
log.error("=============http client error===============");
log.error("json=" + post.getParameters().toString());
log.error(post.getResponseBodyAsString());
log.error("=============================================");
throw new RuntimeException("HTTP请求失败 " + post.getResponseBodyAsString());
}
} finally {
post.releaseConnection();
}
}
public static void main(String[] args) throws Exception {
String reqUrl = "http://testnews.erp.sina.com.cn/index.php/paysettlement/imei_interface/silent_result";
// String reqUrl = "http://testnews.erp.sina.com.cn/index.php/paysettlement/imei_interface/silent_result?is_id=1&is_total=100&is_status=成功&is_updatetime=2016-07-01 11:09:00&is_result_path=''";
}
}