HttpUtils.java
3.56 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
package com.bsth.utils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author hill
* @date
*/
public class HttpUtils {
private final static Logger log = LoggerFactory.getLogger(HttpUtils.class);
public static void request(String url) {
request(url, new CallBack() {
@Override
public void success(byte[] bytes, Map<String, List<String>> header) {
}
@Override
public void failed() {
}
});
}
public static void request(String url, CallBack cb) {
request(url, cb, "GET");
}
public static void request(String url, CallBack cb, String method) {
request(url, cb, method, new HashMap<>());
}
public static void request(String url, CallBack cb, String method, Map<String, String> header) {
request(url, cb, method, header, new HashMap<>());
}
public static void request(String url, CallBack cb, String method, Map<String, String> header, Map<String, Object> param) {
InputStream in = null;
OutputStream out = null;
HttpURLConnection con = null;
long start = System.currentTimeMillis();
try {
con = (HttpURLConnection)new URL(url).openConnection();
con.setRequestMethod(method);
con.setRequestProperty("keep-alive", "true");
con.setRequestProperty("accept", "application/json");
con.setRequestProperty("content-type", "application/json");
con.setDoInput(true);
con.setDoOutput(true);
con.setReadTimeout(5000);
con.setConnectTimeout(5000);
if (header.size() > 0) {
for (Map.Entry<String, String> entry : header.entrySet()) {
con.setRequestProperty(entry.getKey(), entry.getValue());
}
}
if (param.size() > 0) {
out = con.getOutputStream();
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Object> entry : param.entrySet()) {
sb.append("&").append(entry.getKey()).append("=").append(entry.getValue());
}
out.write(sb.substring(1).getBytes("UTF-8"));
out.flush();
}
in = con.getInputStream();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
IOUtils.copy(in, bout); bout.close();
if (con.getResponseCode() == 200) {
cb.success(bout.toByteArray(), con.getHeaderFields());
} else {
log.info(new String(bout.toByteArray()));
cb.failed();
}
} catch (IOException e) {
// TODO Auto-generated catch block
log.error("cost:" + (System.currentTimeMillis() - start));
log.error("HttpUtils.request异常", e);
cb.failed();
} catch (Exception e) {
log.error("处理异常", e);
} finally {
con.disconnect();
try {
if (in != null) in.close();
if (out != null) out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}