-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpUtils.java
More file actions
169 lines (137 loc) · 4.57 KB
/
Copy pathHttpUtils.java
File metadata and controls
169 lines (137 loc) · 4.57 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
package httpTest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import jodd.net.URLDecoder;
public class HttpUtils {
public static HttpUtilBuilder create(String uri) {
return new HttpUtilBuilder(uri);
}
public static class HttpUtilBuilder {
private String uri;
private Map<String, String> params = new HashMap<>();
private String charset = "utf-8";
private int socketTimeout = 20000;
private int connectTimeout = 10000;
private int connectionRequestTimeout = 10000;
HttpUtilBuilder(String uri) {
this.uri = uri;
}
public HttpUtilBuilder addParam(String k, String v) {
params.put(k, v);
return this;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public Map<String, String> getParams() {
return params;
}
public void setParams(Map<String, String> params) {
this.params = params;
}
public String getCharset() {
return charset;
}
public void setCharset(String charset) {
this.charset = charset;
}
public int getSocketTimeout() {
return socketTimeout;
}
public void setSocketTimeout(int socketTimeout) {
this.socketTimeout = socketTimeout;
}
public int getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
public int getConnectionRequestTimeout() {
return connectionRequestTimeout;
}
public void setConnectionRequestTimeout(int connectionRequestTimeout) {
this.connectionRequestTimeout = connectionRequestTimeout;
}
public String get() {
String content = null;
CloseableHttpClient httpClient = null;
CloseableHttpResponse resp = null;
try {
if (uri.startsWith("https")) {
} else {
httpClient = HttpClients.createDefault();
}
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout).setConnectTimeout(connectTimeout).setSocketTimeout(socketTimeout).build();
StringBuilder b = new StringBuilder(uri);
if (b.indexOf("?") == -1) {
b.append("?");
}
for (Map.Entry<String, String> en : params.entrySet()) {
b.append("&").append(en.getKey()).append("=").append(en.getValue());
}
HttpGet get = new HttpGet(URLDecoder.decode(b.toString(), charset));
get.setConfig(requestConfig);
resp = httpClient.execute(get);
content = EntityUtils.toString(resp.getEntity());
} catch (Exception e) {
e.printStackTrace();
} finally {
HttpClientUtils.closeQuietly(resp);
HttpClientUtils.closeQuietly(httpClient);
}
return content;
}
public void post(byte[] data, HttpResponseHandler handler) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse resp = null;
try {
if (uri.startsWith("https")) {
} else {
httpClient = HttpClients.createDefault();
}
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout).setConnectTimeout(connectTimeout).setSocketTimeout(socketTimeout).build();
HttpPost post = new HttpPost(uri);
post.setConfig(requestConfig);
post.setEntity(new ByteArrayEntity(data));
resp = httpClient.execute(post);
handler.handler(resp);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
HttpClientUtils.closeQuietly(resp);
HttpClientUtils.closeQuietly(httpClient);
}
}
}
static class TestRun implements Runnable {
@Override
public void run() {
// for (int i = 0; i < 100; i++) {
// String c = HttpUtils.create("http://www.baidu.com").addParam("a", "a").get();
String c = HttpUtils.create("http://localhost:8080/ServletTest/App").addParam("username1", "yang").get();
System.out.println(c);
// }
}
}
public static void main(String[] args) {
new Thread(new TestRun()).start();
// new Thread(new TestRun()).start();
}
}