HttpClient库使用示例

HttpClient是apache common下的子项目,用来提供高效的、功能丰富的支持Http协议的客户端编程工具包。

官网

依赖版本:

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.3</version>
</dependency>

代码:

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
333
334
335
336
337
338
339
340
package com.demo.utils.lang;

import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
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.conn.HttpClientConnectionManager;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
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.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
* HTTP请求工具
*
* @author 小天
* @version 1.0.0, 2017/10/25 0025 15:51
*/
public class HttpUtils {

private static Logger logger = null;

public static final String DEFAULT_USER_AGENT = "Mozilla/5.0";

/**
* 连接超时时间
*/
private static int DEFAULT_CONNECTION_TIMEOUT = 2000;

/**
* socket 超时时间
*/
private static int DEFAULT_SOCKET_TIMEOUT = 5000;

private static Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

public static ContentType DEFAULT_CONTENT_TYPE = ContentType.create("text/plain", DEFAULT_CHARSET);

public static SSLConnectionSocketFactory sslConnectionSocketFactory;

static {
logger = LogManager.getLogger(HttpUtils.class);
try {
SSLContext sslcontext = SSLContext.getInstance("SSLv3");
sslcontext.init(null, new TrustManager[]{new SimpleTrustManager()}, null);
sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslcontext, new String[]{
"SSLv3",
"TLSv1",
"TLSv1.1",
"TLSv1.2"
}, null, new SimpleTrustManager());
} catch (NoSuchAlgorithmException e) {
logger.error("", e);
} catch (KeyManagementException e) {
logger.error("", e);
}
}

/**
* 发送get请求,并返回响应内容
*
* @param url 请求url
*
* @return 响应内容,UTF-8编码
*
* @throws java.io.IOException
*/
public static String get(String url) throws IOException, HttpException {
return get(url, DEFAULT_CHARSET);
}

/**
* 发送 get 请求,并返回响应内容。
*
* @param url 请求url
* @param charset 响应结果编码
*
* @return 响应内容,
*
* @throws java.io.IOException
*/
public static String get(String url, Charset charset) throws IOException, HttpException {
return get(url, charset, DEFAULT_CONNECTION_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
}

/**
* 发送get请求,并返回响应内容
*
* @param url 请求url
* @param charset 响应结果编码
* @param connectionTimeout 链接建立超时时间
* @param socketTimeout 数据传输超时时间
*
* @return 响应内容
*
* @throws java.io.IOException
*/
public static String get(String url, Charset charset, Integer connectionTimeout, Integer socketTimeout) throws
IOException, HttpException {
URL requestUrl = new URL(url);
HttpGet httpGet = new HttpGet(url);

CloseableHttpClient httpsClient = null;
try {
httpsClient = getHttpClient(connectionTimeout, socketTimeout);
CloseableHttpResponse response = httpsClient.execute(httpGet);
try {
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if(charset == null){
return EntityUtils.toString(entity, DEFAULT_CHARSET);
} else {
return EntityUtils.toString(entity, charset);
}
} else {
throw new HttpException("请求失败: status=" + statusLine.toString());
}
} finally {
org.apache.http.client.utils.HttpClientUtils.closeQuietly(response);
}
} finally {
org.apache.http.client.utils.HttpClientUtils.closeQuietly(httpsClient);
}
}

/**
* 发送post请求,并返回响应内容。使用默认ContentType {@link #DEFAULT_CONTENT_TYPE}
*
* @param url 请求url
* @param body 请求内容
*
* @return 响应内容,UTF-8编码
*
* @throws java.io.IOException
*/
public static String post(String url, String body) throws IOException, HttpException {
return post(url, body, DEFAULT_CONTENT_TYPE);
}

/**
* 发送post请求,并返回响应内容。使用默认ContentType {@link #DEFAULT_CONTENT_TYPE}
*
* @param url 请求url
* @param body 请求内容
* @param charset 编码
*
* @return 响应内容
*
* @throws java.io.IOException
*/
public static String post(String url, String body, Charset charset) throws IOException, HttpException {
return post(url, body, ContentType.TEXT_PLAIN.withCharset(charset));
}

/**
* 发送post请求,并返回响应内容。
*
* @param url 请求url
* @param body 请求内容
* @param contentType http请求内容类型。例如: "text/plain; charset=UTF-8"
*
* @return 响应内容,
*
* @throws java.io.IOException
*/
public static String post(String url, String body, String contentType) throws IOException, HttpException {
return post(url, body, ContentType.parse(contentType));
}

/**
* 发送post请求,并返回响应内容。
*
* @param url 请求url
* @param body 请求内容
* @param contentType http请求内容类型。例如: "text/plain; charset=UTF-8"
*
* @return 响应内容,
*
* @throws java.io.IOException
*/
public static String post(String url, String body, ContentType contentType) throws IOException, HttpException {
return post(url, body, contentType, DEFAULT_CONNECTION_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
}

/**
* 发送post请求,并返回响应内容。
*
* @param url 请求url
* @param body 请求内容
* @param contentType http请求内容类型。例如: "text/plain; charset=UTF-8"
* @param connectionTimeout 链接建立超时时间
* @param socketTimeout 数据传输超时时间
*
* @return 响应内容,
*
* @throws java.io.IOException
*/
public static String post(String url, String body, ContentType contentType, Integer connectionTimeout, Integer socketTimeout) throws
IOException, HttpException {
logger.debug("requestUrl={}", url);
URL requestUrl = new URL(url);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new StringEntity(body, contentType));

CloseableHttpClient httpsClient = getHttpClient(connectionTimeout, socketTimeout);
try {
CloseableHttpResponse response = httpsClient.execute(httpPost);
try {
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, contentType.getCharset());
} else {
throw new HttpException("请求失败: status=" + statusLine.toString());
}
} finally {
org.apache.http.client.utils.HttpClientUtils.closeQuietly(response);
}
} finally {
org.apache.http.client.utils.HttpClientUtils.closeQuietly(httpsClient);
}
}

/**
* 创建一个新的 CloseableHttpClient,该HttpClient只能用于单个请求,不支持cookie,不支持压缩。<br>
* 如果jvm 不支持 SSLv3 算法,该Client将不能用于https请求
*
* @param connectionTimeout 链接建立超时时间
* @param socketTimeout 数据传输超时时间
*
* @return CloseableHttpClient
*/
public static CloseableHttpClient getHttpClient(Integer connectionTimeout, Integer socketTimeout) {

return getHttpClient(connectionTimeout, socketTimeout, null);
}

/**
* 创建一个新的 CloseableHttpClient,该HttpClient只能用于单个请求,不支持cookie,不支持压缩。<br>
* 如果jvm 不支持 SSLv3 算法,该Client将不能用于https请求
*
* @param connectionTimeout 链接建立超时时间
* @param socketTimeout 数据传输超时时间
* @param connectionManager 连接管理器
*
* @return CloseableHttpClient
*/
public static CloseableHttpClient getHttpClient(Integer connectionTimeout, Integer socketTimeout, HttpClientConnectionManager connectionManager) {

RequestConfig.Builder requestConfigBuiler = RequestConfig.custom()
.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT)
.setSocketTimeout(DEFAULT_SOCKET_TIMEOUT);

if(connectionTimeout != null){
requestConfigBuiler.setConnectTimeout(connectionTimeout);
}
if(socketTimeout != null){
requestConfigBuiler.setSocketTimeout(socketTimeout);
}

HttpClientBuilder builder = HttpClients.custom()
.setUserAgent(DEFAULT_USER_AGENT)
.disableCookieManagement()
.disableAuthCaching()
.disableAutomaticRetries()
.disableConnectionState()
.disableContentCompression()
.disableRedirectHandling()
.setDefaultRequestConfig(requestConfigBuiler.build());
if(connectionManager != null){
builder.setConnectionManager(connectionManager);
}
builder.setSSLSocketFactory(sslConnectionSocketFactory);
return builder.build();
}

/**
* 简单ssl 处理, 不作任何验证处理
*/
private static class SimpleTrustManager implements X509TrustManager, HostnameVerifier {

private SimpleTrustManager() {
}

@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
}

@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
}

@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}

@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
}

public static void main(String[] args) {
try {
String requestBody = "<?xml version=\"1.0\" encoding=\"GBK\"?>" + "<AIPG><INFO><TRX_CODE>211003</TRX_CODE><VERSION>03</VERSION><DATA_TYPE>2</DATA_TYPE><LEVEL>5</LEVEL><USER_NAME>20060400000044502</USER_NAME><USER_PASS>`12qwe</USER_PASS><REQ_SN>1505207167678-K0wkdr</REQ_SN><SIGNED_MSG>89dec912f51f5ef8ab06cc503bddfec076de72b515b5802f38a1f7c0ac76c63f900220fb7db4d7928d4d4218108316e32d6b86e01ab56160e11cb511b62593a291816854437d9a1577e53ae32aa575bb4fd0f52bac03094a215508e8352587e0bb289d30dcd0c73d6fc85091d8f69ff3e75a3b78873b092b204846be1a1437bc</SIGNED_MSG></INFO><VALIDR><MERCHANT_ID>200604000000445</MERCHANT_ID><SUBMIT_TIME>20170912170607</SUBMIT_TIME><BANK_CODE>0105</BANK_CODE><ACCOUNT_NO>6217000340000565982</ACCOUNT_NO><ACCOUNT_NAME>杨晓晖</ACCOUNT_NAME><ACCOUNT_PROP>0</ACCOUNT_PROP><ID_TYPE>0</ID_TYPE><ID>110101198601040010</ID><TEL></TEL><REMARK></REMARK></VALIDR></AIPG>";
String res = post("https://113.108.182.3/aipg/ProcessServlet";, requestBody, Charsets.GBK);
System.out.println(res);

res = get("https://oa.jubaopay.com/admin/oalogin.htm";);
System.out.println(res);

} catch (IOException e) {
e.printStackTrace();
} catch (HttpException e) {
e.printStackTrace();
}
}
}

HttpClient http