Lmxy1990 ' Blog

apache httpclient工具类

httpclient的简单封装


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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
public class IntelliHttpClientUtils {

private static final Logger logger = LoggerFactory.getLogger(IntelliHttpClientUtils.class);
//HttpClient
private volatile static CloseableHttpClient httpClient;
//设置请求和传输超时时间 1000*6 秒
private static final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(0b1011101110000).setConnectTimeout(0b1011101110000).build();


/**
* HTTP GET
*
* @param url
* @param encode
* @return
*/
public static String httpGet(String url, String encode) {
if (encode == null) encode = "utf-8";
String resultStr = null;
CloseableHttpClient closeableHttpClient = getHttpClient();
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(requestConfig);
CloseableHttpResponse httpResponse = null;
try {
httpResponse = closeableHttpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() < HttpStatus.SC_BAD_REQUEST) {
HttpEntity entity = httpResponse.getEntity();
resultStr = EntityUtils.toString(entity, encode);
}
//请求失败 直接返回null
} catch (Exception e) {
logger.info("Intelli.IntelliHttpClientUtils.httpGet 产生异常:{}", e.getMessage());
} finally {
HttpClientUtils.closeQuietly(httpResponse);
}
return resultStr;
}


/**
* HTTP POST 请求
*
* @param url
* @param encode
* @return
*/
public static String httpPost(String url, Map<String, String> params, String encode) {
if (encode == null) {
encode = "utf-8";
}
CloseableHttpClient closeableHttpClient = getHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
//组织请求参数
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
Set<String> keySet = params.keySet();
for (String key : keySet) {
paramList.add(new BasicNameValuePair(key, params.get(key)));
}
try {
httpPost.setEntity(new UrlEncodedFormEntity(paramList, encode));
} catch (UnsupportedEncodingException e1) {
logger.info("Intelli.IntelliHttpClientUtils.httpPost (Map请求添加参数时)产生异常:{}", e1.getMessage());
return null;
}
String resultStr = getResponse2Str(encode, closeableHttpClient, httpPost);

return resultStr;
}

/**
* Http Post 请求
*
* @param url
* @param jsonStr
* @param encode
* @return
*/
public static String httpPost(String url, String jsonStr, String encode) {
if (encode == null) {
encode = "utf-8";
}
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-type", "application/json");

//组织请求参数
StringEntity stringEntity = new StringEntity(jsonStr, encode);
httpPost.setEntity(stringEntity);
String resultStr = getResponse2Str(encode, closeableHttpClient, httpPost);

return resultStr;
}

/**
* 获取 CloseableHttpClient
*
* @return
*/
private static CloseableHttpClient getHttpClient() {
if (httpClient == null) {
synchronized (IntelliHttpClientUtils.class) {
if (httpClient == null) {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setConnectionManager(new BasicHttpClientConnectionManager());
httpClient = httpClientBuilder.build();
}
}
}
return httpClient;
}


/**
* 关闭httpClient
*/
private void closeHttpClient() {
if (IntelliHttpClientUtils.httpClient == null) return;
HttpClientUtils.closeQuietly(IntelliHttpClientUtils.httpClient);
}

/***
* 关闭httpClient
*/
@Override
protected void finalize() throws Throwable {
this.closeHttpClient();
super.finalize();
}

/**
* 执行 POST 请求
*
* @param encode
* @param closeableHttpClient
* @param httpPost
* @return
*/
private static String getResponse2Str(String encode, CloseableHttpClient closeableHttpClient, HttpPost httpPost) {
CloseableHttpResponse httpResponse = null;
String resultStr = null;
try {
httpResponse = closeableHttpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() < HttpStatus.SC_BAD_REQUEST) {
HttpEntity entity = httpResponse.getEntity();
resultStr = EntityUtils.toString(entity, encode);
}
} catch (Exception e) {
logger.info("Intelli.IntelliHttpClientUtils.getResponse2Str 产生异常:{}", e.getMessage());
} finally {
HttpClientUtils.closeQuietly(httpResponse);
}
return resultStr;
}


}

End

坚持原创技术分享,您的支持将鼓励我继续创作!