HttpUtil.java
9.4 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package com.espeed.reading.util;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
/**
*
* @项目名称: 鹰眼搜搜索系统
* @版权所有: 深圳市科飞时速网络技术有限公司(0755-88843776)
* @技术支持: info@21gmail.com
* @单元名称: http协议GET/POST请求工具类
* @开始时间: 2017-10-14
* @开发人员: 杨志钊
*/
public class HttpUtil {
public static void main(String[] args) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("ip", "183.15.151.144");
System.out.println(doGet("http://115.29.230.132:85/webservice/ip", params));
}
private final static int timeOut = 60 * 1000;
/** POST请求 */
public static Map<String, Object> doPost(String url, Map<String, Object> params) {
Map<String, Object> result = new HashMap<String, Object>();
result.put("responseCode", 100);
result.put("success", true);
result.put("data", null);
result.put("code", 200);
result.put("msg", null);
HttpURLConnection httpUrlConnection = null;
OutputStream out = null;
DataOutputStream dataOutputStream = null;
InputStream in = null;
ByteArrayOutputStream baos = null;
try {
URLConnection urlConnection = new URL(url).openConnection();
httpUrlConnection = (HttpURLConnection) urlConnection;
// 设置是否向httpUrlConnection输出,post请求,参数要放在http正文内,因此需要设为true,
// 默认情况下是false;
httpUrlConnection.setDoOutput(true);
// 设置是否从httpUrlConnection读入,默认情况下是true;
httpUrlConnection.setDoInput(true);
// 忽略缓存
httpUrlConnection.setUseCaches(false);
// 设定请求的方法为"POST",默认是GET
httpUrlConnection.setRequestMethod("POST");
// 设置连接主机超时(单位:毫秒)
httpUrlConnection.setConnectTimeout(timeOut);
// 设置从主机读取数据超时(单位:毫秒)
httpUrlConnection.setReadTimeout(timeOut);
httpUrlConnection.connect();
// 建立输入流,向指向的URL传入参数
String queryString = "";
if (params != null) {
String value;
for (String key : params.keySet()) {
value = "";
if (params.get(key) != null) {
value = params.get(key).toString();
}
queryString += key + "="
+ URLEncoder.encode(value.toString(), "UTF-8")
+ "&";
}
}
if (queryString.length() > 0) {
queryString = queryString
.substring(0, queryString.length() - 1);
out = httpUrlConnection.getOutputStream();
dataOutputStream = new DataOutputStream(out);
dataOutputStream.writeBytes(queryString);
dataOutputStream.flush();
out.flush();
}
// 获得响应状态
int responseCode = httpUrlConnection.getResponseCode();
result.put("responseCode", responseCode);
if (HttpURLConnection.HTTP_OK == responseCode) {
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
in = httpUrlConnection.getInputStream();
while ((len = in.read(buffer)) != -1) {
baos.write(buffer, 0, len);
baos.flush();
}
result.put("success", true);
result.put("data", baos.toString("UTF-8"));
result.put("code", 200);
result.put("msg", "请求成功。");
} else {
result.put("success", false);
result.put("code", 502);
result.put("msg", "请求异常,请求响应码是" + responseCode + "。");
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
in = httpUrlConnection.getInputStream();
if (in != null) {
while ((len = in.read(buffer)) != -1) {
baos.write(buffer, 0, len);
baos.flush();
}
String str = baos.toString("UTF-8");
if (str.trim().length() > 0) {
result.put("msg", result.get("msg") + str);
}
}
}
} catch (SocketTimeoutException e) {
result.put("success", false);
result.put("code", 501);
result.put("msg", "请求超时,若是多次尝试后仍然无法请求,请联系客服。");
} catch (Exception e) {
result.put("success", false);
result.put("code", 500);
result.put("msg",
"请求异常,异常信息:" + e.getClass() + "->" + e.getMessage());
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
result.put("success", false);
result.put("code", 500);
result.put("msg",
"请求异常,异常信息:" + e.getClass() + "->" + e.getMessage());
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
result.put("success", false);
result.put("code", 500);
result.put("msg",
"请求异常,异常信息:" + e.getClass() + "->" + e.getMessage());
}
}
if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException e) {
result.put("success", false);
result.put("code", 500);
result.put("msg",
"请求异常,异常信息:" + e.getClass() + "->" + e.getMessage());
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
result.put("success", false);
result.put("code", 500);
result.put("msg",
"请求异常,异常信息:" + e.getClass() + "->" + e.getMessage());
}
}
if (httpUrlConnection != null) {
httpUrlConnection.disconnect();
}
}
return result;
}
/** GET请求 */
public static Map<String, Object> doGet(String url, Map<String, Object> params) {
Map<String, Object> result = new HashMap<String, Object>();
result.put("responseCode", 100);
result.put("success", true);
result.put("data", null);
result.put("code", 200);
result.put("msg", null);
HttpURLConnection httpUrlConnection = null;
InputStream in = null;
ByteArrayOutputStream baos = null;
try {
// URL传入参数
String queryString = "";
if (params != null) {
String value;
for (String key : params.keySet()) {
value = "";
if (params.get(key) != null) {
value = params.get(key).toString();
}
queryString += key + "="
+ URLEncoder.encode(value.toString(), "UTF-8")
+ "&";
}
}
if (queryString.length() > 0) {
queryString = queryString
.substring(0, queryString.length() - 1);
url = url + "?" + queryString;
}
URLConnection urlConnection = new URL(url).openConnection();
httpUrlConnection = (HttpURLConnection) urlConnection;
// 设置是否向httpUrlConnection输出,post请求,参数要放在http正文内,因此需要设为true,
// 默认情况下是false;
httpUrlConnection.setDoOutput(false);
// 设置是否从httpUrlConnection读入,默认情况下是true;
httpUrlConnection.setDoInput(true);
// 忽略缓存
httpUrlConnection.setUseCaches(false);
// 设定请求的方法为"POST",默认是GET
httpUrlConnection.setRequestMethod("GET");
// 设置连接主机超时(单位:毫秒)
httpUrlConnection.setConnectTimeout(timeOut);
// 设置从主机读取数据超时(单位:毫秒)
httpUrlConnection.setReadTimeout(timeOut);
httpUrlConnection.connect();
// 获得响应状态
int responseCode = httpUrlConnection.getResponseCode();
result.put("responseCode", responseCode);
if (HttpURLConnection.HTTP_OK == responseCode) {
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
in = httpUrlConnection.getInputStream();
while ((len = in.read(buffer)) != -1) {
baos.write(buffer, 0, len);
baos.flush();
}
result.put("success", true);
result.put("data", baos.toString("UTF-8"));
result.put("code", 200);
result.put("msg", "请求成功。");
} else {
result.put("success", false);
result.put("code", 502);
result.put("msg", "请求异常,请求响应码是" + responseCode + "。");
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
in = httpUrlConnection.getInputStream();
if (in != null) {
while ((len = in.read(buffer)) != -1) {
baos.write(buffer, 0, len);
baos.flush();
}
String str = baos.toString("UTF-8");
if (str.trim().length() > 0) {
result.put("msg", result.get("msg") + str);
}
}
}
} catch (SocketTimeoutException e) {
result.put("success", false);
result.put("code", 501);
result.put("msg", "请求超时,若是多次尝试后仍然无法请求,请联系客服。");
} catch (Exception e) {
result.put("success", false);
result.put("code", 500);
result.put("msg",
"请求异常,异常信息:" + e.getClass() + "->" + e.getMessage());
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
result.put("success", false);
result.put("code", 500);
result.put("msg",
"请求异常,异常信息:" + e.getClass() + "->" + e.getMessage());
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
result.put("success", false);
result.put("code", 500);
result.put("msg",
"请求异常,异常信息:" + e.getClass() + "->" + e.getMessage());
}
}
if (httpUrlConnection != null) {
httpUrlConnection.disconnect();
}
}
return result;
}
}