maven引入
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.0.1</version>
</dependency>
httpClient5相比较httpClient4的区别主要是引入的package变了
Java代码
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.apache.hc.core5.net.URIBuilder;
import org.apache.hc.core5.util.Timeout;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class Httpclient5Test {
@Test
public void getUrl(){
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
try {
client = HttpClients.createDefault();
HttpGet httpGet = null;
//无参数请求
// httpGet = new HttpGet("https://www.baidu.com");
//带参数的get请求
URIBuilder uriBuilder = new URIBuilder("https://www.baidu.com/s");
uriBuilder.setParameter("wd", "好");
httpGet = new HttpGet(uriBuilder.build());
// httpGet.setURI(new URI("https://www.baidu.com"));
//配置
RequestConfig config = RequestConfig.custom().setConnectTimeout(Timeout.ofSeconds(2000))
.setConnectionRequestTimeout(Timeout.ofSeconds(2000))
.setResponseTimeout(Timeout.ofSeconds(2000))
.build();
httpGet.setConfig(config);
//http请求header设置
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68");
//发起get请求
response = client.execute(httpGet);
if(response.getCode() != 200){
return;
}
HttpEntity entity = response.getEntity();
String html = EntityUtils.toString(entity, "utf8");
System.out.println(html);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void poolTest(){
//创建连接池管理器
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
//设置最大连接
cm.setMaxTotal(100);
//设置每个主机(域名)的最大连接数(并发量大时,避免某一个使用过大而其他过小)
cm.setDefaultMaxPerRoute(10);
//通过连接池管理器获取链接
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
HttpPost httpPost = new HttpPost("https://search.xx.com/all");
//参数定义
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("keyword", "电影"));
CloseableHttpResponse response = null;
try {
//创建提交的表单对象
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, StandardCharsets.UTF_8);
httpPost.setEntity(formEntity);
//发起请求
response = httpClient.execute(httpPost);
if(response.getCode() != 200){
return;
}
HttpEntity entity = response.getEntity();
String html = EntityUtils.toString(entity, StandardCharsets.UTF_8);
System.out.println(html);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(response != null) response.close();
if(httpClient != null) httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}