HttpClient学习
一、首先在pom中导入httpclient的工具类
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
二、HttpClient在代码中操作步骤
1、首先在代码中创建httpclient连接池
CloseableHttpClient httpClient = HttpClients.createDefault();
2、创建HttpGet对象
HttpGet httpGet = new HttpGet("https://www.baidu.com");
3、使用httpclient连接池执行HttpGet对象,相当于发送get请求
CloseableHttpResponse execute = httpClient.execute(httpGet);
4、获取发送请求的状态码
StatusLine statusLine = execute.getStatusLine();
5、获取实体
HttpEntity entity = execute.getEntity();
6、利用EntityUtils的toString方法将响应实体转化为字符串
String string = EntityUtils.toString(entity, "utf-8");