29
29
import org .apache .http .client .methods .HttpPut ;
30
30
import org .apache .http .client .methods .HttpRequestBase ;
31
31
import org .apache .http .client .utils .URIBuilder ;
32
+ import org .apache .http .entity .ContentType ;
33
+ import org .apache .http .entity .StringEntity ;
32
34
import org .apache .http .impl .client .CloseableHttpClient ;
33
35
import org .apache .http .impl .client .HttpClients ;
36
+ import org .apache .http .message .BasicHeader ;
34
37
import org .apache .http .message .BasicNameValuePair ;
38
+ import org .apache .http .protocol .HTTP ;
35
39
import org .apache .http .util .EntityUtils ;
36
40
37
41
import java .io .IOException ;
38
42
import java .io .UnsupportedEncodingException ;
39
43
import java .util .ArrayList ;
44
+ import java .util .Collections ;
40
45
import java .util .HashMap ;
41
46
import java .util .List ;
47
+ import java .util .Locale ;
42
48
import java .util .Map ;
43
49
import java .util .Objects ;
44
50
import java .util .Set ;
45
51
46
52
public class HttpClientProvider implements AutoCloseable {
47
53
private final CloseableHttpClient httpClient ;
48
54
private static final String ENCODING = "UTF-8" ;
55
+ private static final String APPLICATION_JSON = "application/json" ;
49
56
private static final int CONNECT_TIMEOUT = 6000 * 2 ;
50
57
private static final int SOCKET_TIMEOUT = 6000 * 10 ;
51
58
private static final int INITIAL_CAPACITY = 16 ;
59
+ private static final RequestConfig REQUEST_CONFIG = RequestConfig .custom ()
60
+ .setConnectTimeout (CONNECT_TIMEOUT )
61
+ .setSocketTimeout (SOCKET_TIMEOUT )
62
+ .build ();
52
63
53
64
private HttpClientProvider () {
54
65
httpClient = HttpClients .createDefault ();
@@ -59,9 +70,21 @@ public static HttpClientProvider getInstance() {
59
70
}
60
71
61
72
public HttpResponse execute (String url , String method , Map <String , String > headers , Map <String , String > params ) throws Exception {
62
- if ("POST" .equals (method )) {
73
+ // convert method option to uppercase
74
+ method = method .toUpperCase (Locale .ROOT );
75
+ if (HttpPost .METHOD_NAME .equals (method )) {
63
76
return doPost (url , headers , params );
64
77
}
78
+ if (HttpGet .METHOD_NAME .equals (method )) {
79
+ return doGet (url , headers , params );
80
+ }
81
+ if (HttpPut .METHOD_NAME .equals (method )) {
82
+ return doPut (url , params );
83
+ }
84
+ if (HttpDelete .METHOD_NAME .equals (method )) {
85
+ return doDelete (url , params );
86
+ }
87
+ // if http method that user assigned is not support by http provider, default do get
65
88
return doGet (url , headers , params );
66
89
}
67
90
@@ -73,7 +96,7 @@ public HttpResponse execute(String url, String method, Map<String, String> heade
73
96
* @throws Exception information
74
97
*/
75
98
public HttpResponse doGet (String url ) throws Exception {
76
- return doGet (url , null , null );
99
+ return doGet (url , Collections . emptyMap (), Collections . emptyMap () );
77
100
}
78
101
79
102
/**
@@ -85,7 +108,7 @@ public HttpResponse doGet(String url) throws Exception {
85
108
* @throws Exception information
86
109
*/
87
110
public HttpResponse doGet (String url , Map <String , String > params ) throws Exception {
88
- return doGet (url , null , params );
111
+ return doGet (url , Collections . emptyMap () , params );
89
112
}
90
113
91
114
/**
@@ -100,18 +123,15 @@ public HttpResponse doGet(String url, Map<String, String> params) throws Excepti
100
123
public HttpResponse doGet (String url , Map <String , String > headers , Map <String , String > params ) throws Exception {
101
124
// Create access address
102
125
URIBuilder uriBuilder = new URIBuilder (url );
126
+ // add parameter to uri
103
127
addParameters (uriBuilder , params );
104
-
105
- /**
106
- * setConnectTimeout:Set the connection timeout, in milliseconds.
107
- * setSocketTimeout:The timeout period (ie response time) for requesting data acquisition, in milliseconds.
108
- * If an interface is accessed, and the data cannot be returned within a certain amount of time, the call is simply abandoned.
109
- */
110
- RequestConfig requestConfig = RequestConfig .custom ().setConnectTimeout (CONNECT_TIMEOUT ).setSocketTimeout (SOCKET_TIMEOUT ).build ();
128
+ // create a new http get
111
129
HttpGet httpGet = new HttpGet (uriBuilder .build ());
112
- httpGet .setConfig (requestConfig );
113
-
130
+ // set default request config
131
+ httpGet .setConfig (REQUEST_CONFIG );
132
+ // set request header
114
133
addHeaders (httpGet , headers );
134
+ // return http response
115
135
return getResponse (httpGet );
116
136
}
117
137
@@ -123,7 +143,7 @@ public HttpResponse doGet(String url, Map<String, String> headers, Map<String, S
123
143
* @throws Exception information
124
144
*/
125
145
public HttpResponse doPost (String url ) throws Exception {
126
- return doPost (url , null , null );
146
+ return doPost (url , Collections . emptyMap (), Collections . emptyMap () );
127
147
}
128
148
129
149
/**
@@ -135,7 +155,7 @@ public HttpResponse doPost(String url) throws Exception {
135
155
* @throws Exception information
136
156
*/
137
157
public HttpResponse doPost (String url , Map <String , String > params ) throws Exception {
138
- return doPost (url , null , params );
158
+ return doPost (url , Collections . emptyMap () , params );
139
159
}
140
160
141
161
/**
@@ -148,19 +168,47 @@ public HttpResponse doPost(String url, Map<String, String> params) throws Except
148
168
* @throws Exception information
149
169
*/
150
170
public HttpResponse doPost (String url , Map <String , String > headers , Map <String , String > params ) throws Exception {
171
+ // create a new http get
151
172
HttpPost httpPost = new HttpPost (url );
152
- /**
153
- * setConnectTimeout:Set the connection timeout, in milliseconds.
154
- * setSocketTimeout:The timeout period (ie response time) for requesting data acquisition, in milliseconds.
155
- * If an interface is accessed, and the data cannot be returned within a certain amount of time, the call is simply abandoned.
156
- */
157
- RequestConfig requestConfig = RequestConfig .custom ().setConnectTimeout (CONNECT_TIMEOUT ).setSocketTimeout (SOCKET_TIMEOUT ).build ();
158
- httpPost .setConfig (requestConfig );
173
+ // set default request config
174
+ httpPost .setConfig (REQUEST_CONFIG );
159
175
// set request header
160
176
addHeaders (httpPost , headers );
161
-
162
- // Encapsulate request parameters
177
+ // set request params
163
178
addParameters (httpPost , params );
179
+ // return http response
180
+ return getResponse (httpPost );
181
+ }
182
+
183
+ /**
184
+ * Send a post request with request body and without headers
185
+ * @param url request address
186
+ * @param body request body conetent
187
+ * @return http response result
188
+ * @throws Exception information
189
+ */
190
+ public HttpResponse doPost (String url , String body ) throws Exception {
191
+ return doPost (url , Collections .emptyMap (), body );
192
+ }
193
+
194
+ /**
195
+ * Send a post request with request headers and request body
196
+ * @param url request address
197
+ * @param headers request header map
198
+ * @param body request body content
199
+ * @return http response result
200
+ * @throws Exception information
201
+ */
202
+ public HttpResponse doPost (String url , Map <String , String > headers , String body ) throws Exception {
203
+ // create a new http post
204
+ HttpPost httpPost = new HttpPost (url );
205
+ // set default request config
206
+ httpPost .setConfig (REQUEST_CONFIG );
207
+ // set request header
208
+ addHeaders (httpPost , headers );
209
+ // add body in request
210
+ addBody (httpPost , body );
211
+ // return http response
164
212
return getResponse (httpPost );
165
213
}
166
214
@@ -172,7 +220,7 @@ public HttpResponse doPost(String url, Map<String, String> headers, Map<String,
172
220
* @throws Exception information
173
221
*/
174
222
public HttpResponse doPut (String url ) throws Exception {
175
- return doPut (url , null );
223
+ return doPut (url , Collections . emptyMap () );
176
224
}
177
225
178
226
/**
@@ -184,12 +232,13 @@ public HttpResponse doPut(String url) throws Exception {
184
232
* @throws Exception information
185
233
*/
186
234
public HttpResponse doPut (String url , Map <String , String > params ) throws Exception {
187
-
235
+ // create a new http put
188
236
HttpPut httpPut = new HttpPut (url );
189
- RequestConfig requestConfig = RequestConfig . custom (). setConnectTimeout ( CONNECT_TIMEOUT ). setSocketTimeout ( SOCKET_TIMEOUT ). build ();
190
- httpPut .setConfig (requestConfig );
191
-
237
+ // set default request config
238
+ httpPut .setConfig (REQUEST_CONFIG );
239
+ // set request params
192
240
addParameters (httpPut , params );
241
+ // return http response
193
242
return getResponse (httpPut );
194
243
}
195
244
@@ -201,10 +250,11 @@ public HttpResponse doPut(String url, Map<String, String> params) throws Excepti
201
250
* @throws Exception information
202
251
*/
203
252
public HttpResponse doDelete (String url ) throws Exception {
204
-
253
+ // create a new http delete
205
254
HttpDelete httpDelete = new HttpDelete (url );
206
- RequestConfig requestConfig = RequestConfig .custom ().setConnectTimeout (CONNECT_TIMEOUT ).setSocketTimeout (SOCKET_TIMEOUT ).build ();
207
- httpDelete .setConfig (requestConfig );
255
+ // set default request config
256
+ httpDelete .setConfig (REQUEST_CONFIG );
257
+ // return http response
208
258
return getResponse (httpDelete );
209
259
}
210
260
@@ -244,7 +294,7 @@ private void addParameters(URIBuilder builder, Map<String, String> params) {
244
294
if (Objects .isNull (params ) || params .isEmpty ()) {
245
295
return ;
246
296
}
247
- params .forEach (( k , v ) -> builder . setParameter ( k , v ) );
297
+ params .forEach (builder :: setParameter );
248
298
}
249
299
250
300
private void addParameters (HttpEntityEnclosingRequestBase request , Map <String , String > params ) throws UnsupportedEncodingException {
@@ -267,7 +317,14 @@ private void addHeaders(HttpRequestBase request, Map<String, String> headers) {
267
317
if (Objects .isNull (headers ) || headers .isEmpty ()) {
268
318
return ;
269
319
}
270
- headers .forEach ((k , v ) -> request .addHeader (k , v ));
320
+ headers .forEach (request ::addHeader );
321
+ }
322
+
323
+ private void addBody (HttpEntityEnclosingRequestBase request , String body ) {
324
+ request .addHeader (HTTP .CONTENT_TYPE , APPLICATION_JSON );
325
+ StringEntity entity = new StringEntity (body , ContentType .APPLICATION_JSON );
326
+ entity .setContentEncoding (new BasicHeader (HTTP .CONTENT_TYPE , APPLICATION_JSON ));
327
+ request .setEntity (entity );
271
328
}
272
329
273
330
@ Override
0 commit comments