-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathHttpUtil.java
More file actions
214 lines (195 loc) · 6.75 KB
/
HttpUtil.java
File metadata and controls
214 lines (195 loc) · 6.75 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
package org.remonde.saasBack.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;
import javax.net.ssl.SSLContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import com.alibaba.fastjson.JSONObject;
public class HttpUtil {
private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
public static String readJSONString(HttpServletRequest request) {
String method = request.getMethod();
if (method == "GET") {
return request.getQueryString();
} else {
StringBuffer json = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null) {
json.append(line);
}
} catch (Exception e) {
System.out.println(e.toString());
}
return json.toString();
}
}
public static JSONObject readJSONParam(HttpServletRequest request) {
String method = request.getMethod();
if (method == "GET") {
return JSONObject.parseObject(request.getQueryString());
} else {
StringBuffer json = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null) {
json.append(line);
}
} catch (Exception e) {
System.out.println(e.toString());
}
return JSONObject.parseObject(json.toString());
}
}
public static ResponseEntity<String> GetResponseEntity(String result) {
HttpHeaders responseHeaders = new HttpHeaders();
MediaType mediaType = new MediaType("text", "html", Charset.forName("UTF-8"));
responseHeaders.setContentType(mediaType);
return new ResponseEntity<String>(result, responseHeaders, HttpStatus.OK);
}
/**
* 获得一个忽略证书的HttpClient
*
* @return
* @throws Exception
*/
public static HttpClient getCertificateValidationIgnoredHttpClient() throws Exception {
SSLContext sslContext = SSLContexts.custom()
// 忽略掉对证书的校验
.loadTrustMaterial(new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
CloseableHttpClient client = HttpClients.custom().setSSLContext(sslContext)
.setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
return client;
}
public static JSONObject doGetRequest(String requestUrl) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {
URL url = new URL(requestUrl);
// http协议传输
HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// 设置请求方式(GET/POST)
httpUrlConn.setRequestMethod("GET");
httpUrlConn.connect();
// 将返回的输入流转换成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.parseObject(buffer.toString());
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
/**
* post请求
*
* @param url
* @param json
* @return
*/
public static JSONObject doPostRequest(String requestUrl, JSONObject json) {
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(requestUrl);
JSONObject response = null;
try {
// ContentType contentType =
// ContentType.create(ContentType.DEFAULT_TEXT,Charsets.UTF_8);
StringEntity s = new StringEntity(json.toString(), ContentType.APPLICATION_JSON);
s.setContentEncoding("UTF-8");
s.setContentType("application/json");// 发送json数据需要设置contentType
post.setEntity(s);
HttpResponse res = client.execute(post);
if (res.getStatusLine().getStatusCode() == org.apache.http.HttpStatus.SC_OK) {
HttpEntity entity = res.getEntity();
String result = EntityUtils.toString(res.getEntity());// 返回json格式:
response = JSONObject.parseObject(result);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;
}
/**
* 返回byte[] 字节数据 post 请求
*
* @param requestUrl
* @param json
* @return
*/
public static byte[] doPostRequestByte(String requestUrl, JSONObject json) throws Exception {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(requestUrl);
byte[] result = null;
try {
logger.info("数据 post 请求");
// ContentType contentType =
// ContentType.create(ContentType.DEFAULT_TEXT,Charsets.UTF_8);
StringEntity s = new StringEntity(json.toString(), ContentType.APPLICATION_JSON);
s.setContentEncoding("UTF-8");
s.setContentType("application/json");// 发送json数据需要设置contentType
post.setEntity(s);
logger.info("数据 post 请求1");
HttpResponse res = client.execute(post);
logger.info("结果:" + res.getStatusLine().getStatusCode());
logger.info("结果:" + res);
if (res.getStatusLine().getStatusCode() == org.apache.http.HttpStatus.SC_OK) {
HttpEntity entity = res.getEntity();
result = EntityUtils.toByteArray(res.getEntity());// 返回图片二进制格式:
}
} catch (Exception e) {
logger.info("post错误" + e);
throw new RuntimeException(e);
}
logger.info("fanhui", result);
return result;
}
}