-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHttpClientUtil.java
52 lines (46 loc) · 2.08 KB
/
HttpClientUtil.java
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
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.jboss.logging.Logger;
public class HttpClientUtil {
protected static final Logger logger = Logger.getLogger(HttpClientUtil.class);
public static <T> T getRequest(String uri, final ResponseHandler<? extends T> responseHandler) {
logger.infov("URI = {0}", uri);
try(CloseableHttpClient httpclient = HttpClients.createDefault()){
HttpGet get = new HttpGet(uri);
return httpclient.execute(get, responseHandler);
}catch(IOException e) {
throw new RuntimeException("Post request failed.", e);
}
}
public static <T> T postRequest(String uri, Map<String, String> params, final ResponseHandler<? extends T> responseHandler) {
logger.infov("URI = {0}", uri);
logger.infov("Params = {0}", params);
try(CloseableHttpClient httpclient = HttpClients.createDefault()){
HttpPost post = new HttpPost(uri);
List<NameValuePair> arguments = convertToNameValuePair(params);
post.setEntity(new UrlEncodedFormEntity(arguments));
return httpclient.execute(post, responseHandler);
}catch(IOException e) {
throw new RuntimeException("Post request failed.", e);
}
}
private static List<NameValuePair> convertToNameValuePair(Map<String, String> params) {
List<NameValuePair> arguments = new ArrayList<>();
if (params != null) {
for (String key : params.keySet()) {
arguments.add(new BasicNameValuePair(key, params.get(key)));
}
}
return arguments;
}
}