HttpClient的使用

HttpClient 4.x API常用方法


HttpClient是由Apache HttpComponents™项目负责创建和维护专注于HTTP和相关协议的低级Java组件工具集。该项目在Apache Software Foundation(http://www.apache.org)下运行,是更大的开发人员和用户社区的一部分。

HttpClient的特性

  • 基于标准、纯净的 Java 语言。实现了 HTTP 1.0 和 HTTP 1.1
  • 以可扩展的面向对象的结构实现了 HTTP 全部的方法(GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE)。
  • 支持 HTTPS 协议。
  • 通过 HTTP 代理建立透明的连接。
  • 利用 CONNECT 方法通过 HTTP 代理建立隧道的 HTTPS 连接。
  • Basic, Digest, NTLMv1, NTLMv2, NTLM2 Session, SNPNEGO/Kerberos 认证方案。
  • 插件式的自定义认证方案。
  • 便携可靠的套接字工厂使它更容易的使用第三方解决方案。
  • 连接管理器支持多线程应用。支持设置最大连接数,同时支持设置每个主机的最大连接数,发现并关闭过期的连接。
  • 自动处理 Set-Cookie 中的 Cookie。
  • 插件式的自定义 Cookie 策略。
  • Request 的输出流可以避免流中内容直接缓冲到 Socket 服务器。
  • Response 的输入流可以有效的从 Socket 服务器直接读取相应内容。
  • 在 HTTP 1.0 和 HTTP 1.1 中利用 KeepAlive 保持持久连接。
  • 直接获取服务器发送的 response code 和 headers。
  • 设置连接超时的能力。
  • 实验性的支持 HTTP 1.1 response caching。
  • 源代码基于 Apache License 可免费获取。

HttpClient 使用流程

  1. 创建HttpClient对象

    1
    CloseableHttpClient httpClient = HttpClients.createDefault();
  2. 创建请求方法实例,并制定请求URL。如果需要发送GET请求,创建HttpGet对象,如果需要发送POST请求,创建HttpPost对象

    1
    2
    3
    4
    5
    Get:
    HttpGet httpGet = new HttpGet("http://apis.juhe.cn/ip/ipNew?ip=112.112.11.11&key=56e8d1ce739a1016392097d58af41af4");
    Post:
    HttpPost httpPost = new HttpPost("http://apis.juhe.cn/ip/ipNew");

  3. 如果要发送请求参数,可调用HttpGetHttpPost共同的setParams(HttpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数

    HttpClient提供一个UrlEncodedFormEntity()类以方便Post请求传递参数,UrlEncodedFormEntit继承StringEntity类,它提供了四个构造函数

    • UrlEncodedFormEntity(List<? extends NameValuePair> parameters, String charset)
    • UrlEncodedFormEntity(Iterable<? extends NameValuePair> parameters, Charset charset)
    • UrlEncodedFormEntity(List<? extends NameValuePair> parameters)
    • UrlEncodedFormEntity(Iterable<? extends NameValuePair> parameters)
  4. 可以通过调用HttpClient 对象的execute(HttpUriRequest request)方法来发送请求,这个方法返回一个HttpResponse

  5. 调用HttpResponsegetAllHeaders()getHeaders(String name)等方法可获得服务器的响应头;调用HttpResponergetEntity方法可获取HttpEntity对象,该对象包装了服务器的响应内容,可通过该对象获取服务器的响应内容

  6. 不管执行方法是否成功,都应该释放链接

使用实例


maven依赖如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- Apache Http Begin -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.5</version>
</dependency>
<!-- Apache Http End -->

创建GET请求:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class MyTest {
public static void main(String[] args) {
get();
}

private static void get() {
// 创建 HttpClient 客户端
CloseableHttpClient httpClient = HttpClients.createDefault();

// 创建 HttpGet 请求
HttpGet httpGet = new HttpGet("http://localhost:8080/content/page?draw=1&start=0&length=10");
// 设置长连接
httpGet.setHeader("Connection", "keep-alive");
// 设置代理(模拟浏览器版本)
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
// 设置 Cookie
httpGet.setHeader("Cookie", "UM_distinctid=16442706a09352-0376059833914f-3c604504-1fa400-16442706a0b345; CNZZDATA1262458286=1603637673-1530123020-%7C1530123020; JSESSIONID=805587506F1594AE02DC45845A7216A4");

CloseableHttpResponse httpResponse = null;
try {
// 请求并获得响应结果
httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
// 输出请求结果
System.out.println(EntityUtils.toString(httpEntity));
} catch (IOException e) {
e.printStackTrace();
}

// 无论如何必须关闭连接
finally {
if (httpResponse != null) {
try {
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}

if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

创建POST请求:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public class MyTest {
public static void main(String[] args) {
post();
}

private static void post() {
// 创建 HttpClient 客户端
CloseableHttpClient httpClient = HttpClients.createDefault();

// 创建 HttpPost 请求
HttpPost httpPost = new HttpPost("http://localhost:8080/content/page");
// 设置长连接
httpPost.setHeader("Connection", "keep-alive");
// 设置代理(模拟浏览器版本)
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
// 设置 Cookie
httpPost.setHeader("Cookie", "UM_distinctid=16442706a09352-0376059833914f-3c604504-1fa400-16442706a0b345; CNZZDATA1262458286=1603637673-1530123020-%7C1530123020; JSESSIONID=805587506F1594AE02DC45845A7216A4");

// 创建 HttpPost 参数
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("draw", "1"));
params.add(new BasicNameValuePair("start", "0"));
params.add(new BasicNameValuePair("length", "10"));

CloseableHttpResponse httpResponse = null;
try {
// 设置 HttpPost 参数
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// 输出请求结果
System.out.println(EntityUtils.toString(httpEntity));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

// 无论如何必须关闭连接
finally {
try {
if (httpResponse != null) {
httpResponse.close();
}
} catch (IOException e) {
e.printStackTrace();
}

try {
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

封装出的工具类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
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 java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class HttpClientUtils {
public static final String CONNECTION = "Connection";
public static final String USER_AGENT= "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36";

public static String doGet(String url,String cookie){
return createRequest(HttpGet.METHOD_NAME,url,cookie);
}

public static String doGet(String url){
return createRequest(HttpGet.METHOD_NAME,url,null);
}

private static String createRequest(String method, String url, String cookie, BasicNameValuePair ... basicNameValuePairs){
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = null;
String result = null;
try {
if (HttpGet.METHOD_NAME.equals(method)) {
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Connection", CONNECTION);
httpGet.setHeader("User-Agent", USER_AGENT);
if (StringUtils.isNotBlank(cookie)) {
httpGet.setHeader("Cookie", cookie);
}
httpResponse = closeableHttpClient.execute(httpGet);
result = EntityUtils.toString(httpResponse.getEntity());
} else if (HttpPost.METHOD_NAME.equals(method)) {
HttpPost httpPost = new HttpPost();
httpPost.setHeader("Connection", CONNECTION);
httpPost.setHeader("User-Agent", USER_AGENT);
if (StringUtils.isNotBlank(cookie)) {
httpPost.setHeader("Cookie", cookie);
}

httpPost.setEntity(new UrlEncodedFormEntity(Arrays.asList(basicNameValuePairs), "UTF-8"));

httpResponse = closeableHttpClient.execute(httpPost);
System.out.println(httpResponse);
result = EntityUtils.toString(httpResponse.getEntity());
}
}catch (IOException e) {
e.printStackTrace();
}finally {
if (httpResponse!=null){
try {
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (closeableHttpClient!=null) {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
return result;
}
}