工具类HttpClientUtil

HttpClientUtil

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.jinbei.common.util;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @program: UnifiedBgManage
* @description: HttpClient工具类
* @author: WYuyin
* @create: 2018-11-22 13:18
**/
public class HttpClientUtil {

private static CloseableHttpClient client = HttpClientBuilder.create().build();

public static String doGet(String url, Map<String, String> param,Map<String,String> headers) {
String resultString = "";
CloseableHttpResponse response = null;
try {
URIBuilder uri = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
uri.addParameter(key, param.get(key));
}
}
HttpGet get = new HttpGet(uri.build());
if (headers != null){
for (String key :headers.keySet()) {
get.setHeader(key,headers.get(key));
}
}

response = client.execute(get);

if (response.getStatusLine().getStatusCode() == 200){
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}

} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (response != null){
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}

public static String doGet(String url){
return doGet(url,null,null);
}

public static String doGet(String url, Map<String, String> param){
return doGet(url,param,null);
}

public static String doPost(String url, Map<String, String> param,Map<String,String> headers){
String resultString = "";
CloseableHttpResponse response = null;
try {
HttpPost httpPost = new HttpPost(url);
if (param != null){
List<NameValuePair> paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8");
httpPost.setEntity(entity);
}
if (headers != null){
for (String key :headers.keySet()) {
httpPost.setHeader(key,headers.get(key));
}
}

response = client.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(),"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}

public static String doPost(String url, Map<String, String> param) {
return doPost(url,param,null);
}
public static String doPost(String url){
return doPost(url,null,null);
}
}

最好使用doPost,doGet不太安全

HttpClient的精简用法

之前学习了HttpClient的用法,也自己包了一个工具类,前两天看了大佬的代码,

1546915517113

我称其为HttpClient的精简用法吧,这省了很多事情,起码自己不用去归纳一套流程了,熟练了也是非常方便的,而且还适应了各种情景:加请求头,参数,编码,timeout时间,真的是非常便捷了,emm 请忽略我截图里面多了一个bodyForm的操作。

改响应数据编码为utf-8我找了好久,最后就在自动显示的所有方法里看到有个加Charset参数的,开心(^▽^)