-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpUtil.java
More file actions
41 lines (29 loc) · 1.26 KB
/
HttpUtil.java
File metadata and controls
41 lines (29 loc) · 1.26 KB
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
package th.util;
import java.io.IOException;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.google.gson.Gson;
public class HttpUtil {
private final static String CONTENT_TYPE_TEXT_JSON = "text/json";
public static String postRequest(String url, Map<String, Object> param) throws ClientProtocolException, IOException{
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
Gson gson = new Gson();
String parameter = gson.toJson(param);
StringEntity se = new StringEntity(parameter);
se.setContentType(CONTENT_TYPE_TEXT_JSON);
httpPost.setEntity(se);
CloseableHttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
return result;
}
}