主要参考了 Baeldung 的 HttpClient 4 Cookbook
创建 HttpClient
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
发送 GET
请求
HttpGet httpGet = new HttpGet("http://www.baidu.com");
CloseableHttpResponse response = httpClient.execute(httpGet);
获取 HTTP Response
的状态码
CloseableHttpResponse response = httpClient.execute(httpGet);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
获取 HTTP Response
的 Media Type
CloseableHttpResponse response = httpClient.execute(httpGet);
String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType();
Assert.assertEquals(ContentType.TEXT_HTML.getMimeType(), contentMimeType);
获取 HTTP Response
的内容
CloseableHttpResponse response = httpClient.execute(httpGet);
String bodyAsString = EntityUtils.toString(response.getEntity());
Assert.assertNotNull(bodyAsString);
获取 HTTP Response
响应头部信息
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet("http://localhost:8080/hello");
CloseableHttpResponse response = httpClient.execute(httpGet);
Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);
Assert.assertNotSame(0,headers.length);
设置请求的超时时间
HttpGet httpGet = new HttpGet("http://www.baidu.com");
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(1000)
.setConnectTimeout(1000)
.setSocketTimeout(1000).build();
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = httpClient.execute(httpGet);
设置 HttpClient
的超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(1000)
.setConnectTimeout(1000)
.setSocketTimeout(1000).build();
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
HttpGet httpGet = new HttpGet("http://www.baidu.com");
CloseableHttpResponse response = httpClient.execute(httpGet);
设置请求头
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet("http://www.baidu.com");
httpGet.addHeader(HttpHeaders.ACCEPT,"application/xml");
CloseableHttpResponse response = httpClient.execute(httpGet);
发送 POST
请求
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("http://www.baidu.com");
CloseableHttpResponse response = httpClient.execute(httpPost);
模拟表单提交的POST
请求
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
List<NameValuePair> parameters = new ArrayList<>();
parameters.add(new BasicNameValuePair("key1","value1"));
parameters.add(new BasicNameValuePair("key2","value2"));
HttpPost httpPost = new HttpPost("http://www.baidu.com");
httpPost.setEntity(new UrlEncodedFormEntity(parameters,Consts.UTF_8));
CloseableHttpResponse response = httpClient.execute(httpPost);
处理HTTP
重定向
HttpClient
默认策略会处理重定向请求,使用disableRedirectHandling()
可以禁用重定向处理策略;
CloseableHttpClient instance = HttpClientBuilder.create().disableRedirectHandling().build();
CloseableHttpResponse response = instance.execute(new HttpGet("https://dwz.cn/IKeCGtaO"));
Assert.assertEquals(HttpStatus.SC_MOVED_TEMPORARILY,response.getStatusLine().getStatusCode());
关闭/释放资源
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet("http://www.baidu.com");
response = httpClient.execute(httpGet);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
HttpEntity httpEntity = response.getEntity();
if (null != httpEntity) {
InputStream inputStream = httpEntity.getContent();
inputStream.close();
}
} catch (IOException e) {
} finally {
response.close();
HttpClientUtils.closeQuietly(httpClient);
}
项目代码
码云链接:httpClient 代码示例
网友评论