一 介绍
1.通过使用PoolingHttpClientConnectionManager类来创建连接池管理器对象,用来设置HttpClient池中的数量,而不是每次使用都去重新创建一个HttpClient对象,以为这样会浪费资源。在发起请求前通过HttpClients的custom()方法来创建一个HttpClientBuilder对象,再通过HttpClientBuilder对象的setConnectionManager(PoolingHttpClientConnectionManager phccm)方法来完成连接池管理对象的设置。
2.在发起请求前我们可以通过RequestConfig类来设置请求的连接超时时间和Socket超时时间。
3.对于请求参数我们使用List< NameValuePair>用来封装请求参数,NameValuePair是一个接口,当我们向list 里面存放参数时,我们可以使用实现了NameValuePair的BasicNameValuePair来存放参数,在参数list创建完成之后使用UrlEncodedFormEntity表单对象类来封装参数,继而发出请求。
代码
pom依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<!--<scope>test</scope>-->
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
请求代码
public static void main(String[] args) {
//创建HttpClient连接池管理器
PoolingHttpClientConnectionManager httpClientConnectionManager=new PoolingHttpClientConnectionManager();
//设置最大连接数-->连接池中HttpClient对象的个数
httpClientConnectionManager.setMaxTotal(200);
//设置目的主机的最大连接数-->多个目的主机时,一个目的主机使用的HttpClient最大个数
httpClientConnectionManager.setDefaultMaxPerRoute(5);
//发起请求
doGet(httpClientConnectionManager);
}
public static void doGet(PoolingHttpClientConnectionManager poolingHttpClientConnectionManager){
//从连接池中获取HttpClient对象
CloseableHttpClient httpClient= HttpClients.custom().setConnectionManager(poolingHttpClientConnectionManager).build();
//配置请求信息
RequestConfig requestConfig=RequestConfig.custom().setConnectionRequestTimeout(5*1000).setSocketTimeout(10*1000).build();
//发起请求
HttpGet httpGet=new HttpGet("http://www.baidu.com");
httpGet.setConfig(requestConfig);
CloseableHttpResponse httpResponse=null;
try {
httpResponse=httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode()==200){
String content= EntityUtils.toString(httpResponse.getEntity());
System.out.println(content);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (httpResponse!=null){
try {
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void doPost(PoolingHttpClientConnectionManager poolingHttpClientConnectionManager)throws UnsupportedEncodingException{
//从连接池中获取HttpClient对象
CloseableHttpClient httpClient= HttpClients.custom().setConnectionManager(poolingHttpClientConnectionManager).build();
//配置请求信息
RequestConfig requestConfig=RequestConfig.custom().setConnectionRequestTimeout(5*1000).setSocketTimeout(10*1000).build();
//发起请求
HttpPost httpPost=new HttpPost("http://www.baidu.com");
httpPost.setConfig(requestConfig);
//声明集合,封装参数
List<NameValuePair> nameValuePairList=new ArrayList<NameValuePair>();
nameValuePairList.add(new BasicNameValuePair("key1","value1"));
nameValuePairList.add(new BasicNameValuePair("key2","value2"));
//创建表单对象
UrlEncodedFormEntity formEntity=new UrlEncodedFormEntity(nameValuePairList,"utf8");
//将表单对象添加到Post请求中
httpPost.setEntity(formEntity);
CloseableHttpResponse httpResponse=null;
try {
httpResponse=httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode()==200){
String result= EntityUtils.toString(httpResponse.getEntity());
System.out.println(result);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (httpResponse!=null){
try {
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}