导入 JAR 包
如果使用的是 maven 直接加入以下依赖:
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpclientartifactId>
<version>4.5.12version>
dependency>
如果是手动导入 JAR 包需要添加三个包:
- httpcore.jar
- commons-logging.jar
- commons-code.jar
下面分别对网页 GET 请求和 POST 请求案例示例:
GET 请求
package top.shijialeya.test;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import java.io.IOException;
//Get 请求
public class GetTest {
@Test
public void test01() {
//获取到 客户端 对象
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//创建 Get 请求
HttpGet httpGet = new HttpGet("https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=shijialeya");
//响应
CloseableHttpResponse response;
try {
//请求参数可以直接编写在请求地址上
//添加请求头
httpGet.addHeader("User-Agent", "Mozilla AppleWebKit Chrome Safari Edg");
//...
//添加请求配置,设置响应时间等
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(2000) //服务器响应超时时间
.setConnectionRequestTimeout(2000) //连接服务器超时时间
.build();
//将配置信息添加到 httpGet 中
httpGet.setConfig(requestConfig);
//连接,获取网页
response = httpClient.execute(httpGet);
//获取全部的响应头参数
System.out.println("获取全部的响应头参数");
Header[] allHeaders = response.getAllHeaders();
for (int i = 0; i < allHeaders.length; i++) {
String name = allHeaders[i].getName();
String value = allHeaders[i].getValue();
System.out.println(name + ":" + value);
}
System.out.println();
//获取指定的响应头
System.out.println("获取指定的响应头");
Header[] jsessionids = response.getHeaders("Content-Type");
for (Header jsessionid : jsessionids) {
System.out.println("Content-Type:" + jsessionid);
}
//获取到网页内容
System.out.println();
System.out.println("获取到网页内容");
HttpEntity entity = response.getEntity();
//将内容转化为字符串,并且设定指定字符集
String text = EntityUtils.toString(entity, "UTF-8");
System.out.println(text);
} catch (IOException e) {
e.printStackTrace();
} finally {
//资源释放
if (httpGet != null) {
try {
httpGet.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
运行结果:
获取全部的响应头参数
Bdpagetype:3
Bdqid:0xd3d652ee000adc25
Cache-Control:private
Ckpacknum:2
Ckrndstr:e000adc25
Connection:keep-alive
Content-Type:text/html;charset=utf-8
Date:Wed, 18 Nov 2020 14:50:21 GMT
//内容省略...
获取指定的响应头
Content-Type:Content-Type: text/html;charset=utf-8
获取到网页内容
<!DOCTYPE html>
<!--STATUS OK-->
//内容省略...
POST 请求
package top.shijialeya.test;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
public class PostTest {
@Test
public void test01() {
//创建 HTTP 客户端
CloseableHttpClient client = HttpClients.createDefault();
//创建 Post 请求对象
HttpPost post = new HttpPost("http://bp.shijialeya.top");
//创建 Post 请求参数集合
ArrayList<BasicNameValuePair> list = new ArrayList<>();
//添加 Post 请求参数和值
list.add(new BasicNameValuePair("username","jiale123"));
list.add(new BasicNameValuePair("password","123456"));
try {
//将请求参数集合添加到请求中
post.setEntity(new UrlEncodedFormEntity(list,"UTF-8"));
//添加请求头的参数和值
post.addHeader("Cookie", "123456789");
post.addHeader("User-Agent", "Mozilla AppleWebKit Chrome Safari Edg");
//...
//添加请求配置,设置响应时间等
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(2000) //服务器响应超时时间
.setConnectionRequestTimeout(2000) //连接服务器超时时间
.build();
//将配置信息添加到 post
post.setConfig(requestConfig);
//连接,获得内容
CloseableHttpResponse httpResponse = client.execute(post);
//获得响应头的全部参数
System.out.println("获得响应头的全部参数");
Header[] allHeaders = httpResponse.getAllHeaders();
for (int i = 0; i < allHeaders.length; i++) {
String name = allHeaders[i].getName();
String value = allHeaders[i].getValue();
System.out.println(name + ":" + value);
}
System.out.println();
//获得单个响应头
System.out.println("获得单个响应头");
Header[] headers = httpResponse.getHeaders("Content-Type");
for (Header header : headers) {
System.out.println("Content-Type:" + header);
}
System.out.println();
//获得网页内容
System.out.println("获得网页内容");
HttpEntity entity = httpResponse.getEntity();
//指定字符集
String text = EntityUtils.toString(entity, "utf-8");
System.out.println(text);
} catch (Exception e) {
e.printStackTrace();
} finally {
//资源释放
if (post != null){
try {
post.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
if (client != null){
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
运行结果:
获得响应头的全部参数
Server:nginx/1.18.0
Date:Wed, 18 Nov 2020 14:56:22 GMT
Content-Type:text/html
Content-Length:1326
Connection:keep-alive
Accept-Ranges:bytes
ETag:W/"1326-1603588766000"
Last-Modified:Sun, 25 Oct 2020 01:19:26 GMT
获得单个响应头
Content-Type:Content-Type: text/html
获得网页内容
<!DOCTYPE html>
<html lang="ZH-cn">
<head>
<meta charset="UTF-8">
//内容省略...
去打王者了,下次再补写