-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpUtil.java
More file actions
324 lines (265 loc) · 11 KB
/
HttpUtil.java
File metadata and controls
324 lines (265 loc) · 11 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
package uyun.show.server.domain.util;
import org.apache.http.entity.mime.content.StringBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import uyun.show.server.domain.exception.ProcessFailException;
import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class HttpUtil {
private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);
public static String get(String url) throws Exception {
return get(url, createHeader());
}
public static String get(String url, Map<String, String> headers) throws Exception {
return get(url, null, headers);
}
public static String get(String url, String cookie, Map<String, String> headers)
throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
if (url.startsWith("https")) {
httpClient = createSSLInsecureClient();
}
String result = null;
HttpGet request = new HttpGet(url);
if (headers != null) {
for (Entry<String, String> entry : headers.entrySet()) {
request.setHeader(entry.getKey(), entry.getValue());
}
}
if (cookie != null) {
request.setHeader("Cookie", cookie);
}
if (logger.isDebugEnabled()) {
logger.debug("executing request to " + url);
}
HttpResponse httpResponse = httpClient.execute(request);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, "utf-8");
}
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (logger.isDebugEnabled()) {
logger.debug("executing request result :{}", result);
}
if (200 != statusCode) {
if (!result.contains("选择的过滤资源不存在")) {
logger.warn("请求返回CODE:" + statusCode + "返回数据:" + result);
}
return "";
}
return result;
}
public static Map<String, String> getstore(String url) throws Exception {
return getstore(url, createHeader());
}
public static Map<String, String> getstore(String url, Map<String, String> headers) throws Exception {
return getstore(url, null, headers);
}
public static Map<String, String> getstore(String url, String cookie, Map<String, String> headers)
throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
if (url.startsWith("https")) {
httpClient = createSSLInsecureClient();
}
String result = null;
HttpGet request = new HttpGet(url);
if (headers != null) {
for (Entry<String, String> entry : headers.entrySet()) {
request.setHeader(entry.getKey(), entry.getValue());
}
}
if (cookie != null) {
request.setHeader("Cookie", cookie);
}
if (logger.isDebugEnabled()) {
logger.debug("executing request to " + url);
}
Map<String, String> resultMap = new HashMap<>();
try {
HttpResponse httpResponse = httpClient.execute(request);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, "utf-8");
}
resultMap.put("data", result);
int statusCode = httpResponse.getStatusLine().getStatusCode();
resultMap.put("statusCode", "" + statusCode);
if (logger.isDebugEnabled()) {
logger.debug("executing request result :{}", result);
}
if (200 != statusCode) {
if (!result.contains("选择的过滤资源不存在")) {
logger.warn("请求返回CODE:" + statusCode + "返回数据:" + result);
}
return resultMap;
}
} catch (Exception e) {
}
return resultMap;
}
public static String post(String url, Map<String, String> headers, String jsonString)
throws Exception {
return post(url, null, headers, jsonString);
}
public static String post(String url, String jsonString)
throws Exception {
return post(url, createHeader(), jsonString);
}
private static Map<String, String> createHeader() {
Map<String, String> headers = new HashMap<>();
headers.put("Accept", "application/json;charset=utf-8");
headers.put("Content-Type", "application/json;charset=utf-8");
return headers;
}
public static String post(String url, String cookie, Map<String, String> headers, String jsonString)
throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = null;
if (url.startsWith("https")) {
httpClient = createSSLInsecureClient();
}
HttpPost postRequest = new HttpPost(url);
if (headers != null) {
for (Entry<String, String> entry : headers.entrySet()) {
postRequest.setHeader(entry.getKey(), entry.getValue());
}
}
if (cookie != null) {
postRequest.setHeader("Cookie", cookie);
}
if (!StringUtils.isBlank(jsonString)) {
StringEntity entity = new StringEntity(jsonString, "utf-8");
postRequest.setEntity(entity);
}
try {
HttpResponse httpResponse = httpClient.execute(postRequest);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (200 != statusCode) {
if (!result.contains("选择的过滤资源不存在")) {
logger.warn("请求返回CODE:" + statusCode + "返回数据:" + result);
}
return result;
}
} catch (Exception e) {
}
return result;
}
public static Map<String, String> poststore(String url, String jsonString)
throws Exception {
return poststore(url, createHeader(), jsonString);
}
public static Map<String, String> poststore(String url, Map<String, String> headers, String jsonString)
throws Exception {
return poststore(url, null, headers, jsonString);
}
public static Map<String, String> poststore(String url, String cookie, Map<String, String> headers, String jsonString) {
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = null;
Map<String, String> resultMap = new HashMap<>();
if (url.startsWith("https")) {
httpClient = createSSLInsecureClient();
}
HttpPost postRequest = new HttpPost(url);
if (headers != null) {
for (Entry<String, String> entry : headers.entrySet()) {
postRequest.setHeader(entry.getKey(), entry.getValue());
}
}
if (cookie != null) {
postRequest.setHeader("Cookie", cookie);
}
if (!StringUtils.isBlank(jsonString)) {
StringEntity entity = new StringEntity(jsonString, "utf-8");
postRequest.setEntity(entity);
}
try {
HttpResponse httpResponse = httpClient.execute(postRequest);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
resultMap.put("data", result);
int statusCode = httpResponse.getStatusLine().getStatusCode();
resultMap.put("statusCode", "" + statusCode);
} catch (Exception e) {
}
return resultMap;
}
private static CloseableHttpClient createSSLInsecureClient() {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (chain, authType) -> {
return true; //信任所有
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (Exception e) {
logger.warn("CertificateException", e);
}
return HttpClients.createDefault();
}
public static String upload(String url, String filename, Map<String, String> params) {
CloseableHttpClient httpclient = HttpClients.createDefault();
String result = null;
try {
// CloseableHttpClient httpclient = HttpClients.createDefault();
if (url.startsWith("https")) {
httpclient = createSSLInsecureClient();
}
HttpPost postRequest = new HttpPost(url);
postRequest.setHeader("apikey", params.get("apikey"));
FileBody bin = new FileBody(new File(filename));
HttpEntity reqEntity = MultipartEntityBuilder.create().
addPart("file", bin).
addPart("tags", new StringBody(params.get("tags"))).
addPart("flag", new StringBody(params.get("flag"))).
addPart("apikey", new StringBody(params.get("apikey"))).
build();
postRequest.setEntity(reqEntity);
CloseableHttpResponse response = httpclient.execute(postRequest);
try {
System.out.println(response.getStatusLine());
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity);
}
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}