1、创建HTTP客户端
- CloseableHttpClient client = HttpClientBuilder.create().build();
2、发送基本的GET请求
- instance.execute(new HttpGet(“http://www.baidu.com”));
3、获取HTTP响应的状态码
- String url = “http://www.baidu.com”;
- CloseableHttpResponse response = instance.execute(new HttpGet(url));
- assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
4、获取响应的媒体类型
- String url = “http://www.baidu.com”;
- CloseableHttpResponse response = instance.execute(new HttpGet(url));
- String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType();
- assertThat(contentMimeType, equalTo(ContentType.TEXT_HTML.getMimeType()));
5、获取响应的BODY部分
- String url = “http://www.baidu.com”;
- CloseableHttpResponse response = instance.execute(new HttpGet(url));
- String bodyAsString = EntityUtils.toString(response.getEntity());
- assertThat(bodyAsString, notNullValue());
6、配置请求的超时设置
- @Test(expected=SocketTimeoutException.class)
- public void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws ClientProtocolException, IOException{
- RequestConfig requestConfig = RequestConfig.custom()
- .setConnectionRequestTimeout(50).setConnectTimeout(50)
- .setSocketTimeout(50).build();
- HttpGet request = new HttpGet(SAMPLE_URL);
- request.setConfig(requestConfig);
- instance.execute(request);
- }
7、发送POST请求
- instance.execute(new HttpPost(SAMPLE_URL));
8、为HTTP请求配置重定向
- CloseableHttpClient instance = HttpClientBuilder.create().disableRedirectHandling().build();
- CloseableHttpResponse response = instance.execute(new HttpGet(SAMPLE_URL));
- assertThat(reponse.getStatusLine().getStatusCode(), equalTo(301));
9、配置请求的HEADER部分
- HttpGet request = new HttpGet(SAMPLE_URL);
- request.addHeader(HttpHeaders.ACCEPT, “application/xml”);
- response = instance.execute(request);
10、获取响应的HEADER部分
- CloseableHttpResponse response = instance.execute(new HttpGet(SAMPLE_URL));
- Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);
- assertThat(headers, not(emptyArray()));
11、关闭或释放资源
- response = instance.execute(new HttpGet(SAMPLE_URL));
- try{
- HttpEntity entity = response.getEntity();
- if(entity!=null){
- InputStream instream = entity.getContent();
- instream.close();
- }
- } finally{
- response.close();
- }
12 URI构造器
URI uri = new URIBuilder().setScheme("http")
.setHost("www.baidu.com")
.setPath("wenku")
.setParameter("name", "xiyouji")
.setParameter("author", "wuchengen").build();
System.out.println(uri); // 结果为http://www.baidu.comwenku?name=xiyouji&author=wuchengen
13 FORM参数
List<NameValuePair> formParamList = new ArrayList<NameValuePair>();
formParamList.add(new BasicNameValuePair("name", "zhangsan"));
formParamList.add(new BasicNameValuePair("age", "20"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formParamList);
HttpPost post = new HttpPost();
post.setEntity(formEntity);
14 String消息体
StringEntity stringEntity = new StringEntity("{\"name\":\"fabregas\"}");
StringEntity stringEntity2 = new StringEntity("{\"name\":\"fabregas\"}", ContentType.APPLICATION_JSON);
StringEntity stringEntity3 = new StringEntity("{\"name\":\"fabregas\"}", Consts.UTF_8);
StringEntity stringEntity4 = new StringEntity("{\"name\":\"fabregas\"}", ContentType.create("application/json", "UTF-8"));
StringEntity stringEntity5 = new StringEntity("{\"name\":\"fabregas\"}", ContentType.APPLICATION_JSON.withCharset(Consts.UTF_8));
15 连接池
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setMaxTotal(200);
connManager.setDefaultMaxPerRoute(10);
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(connManager).build();