eeace8ff7dfe028232cb71cabc64b2f840a4b66d.svn-base
1.47 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
42
43
44
45
package com.phxl.common.util;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSON;
import com.phxl.common.response.PlatFormResponse;
import com.phxl.common.response.PlatFormResponseConstant;
public class HTTPClient {
private static HttpClient client;
//使用POST方法发送JSON数据
public static String sendJsonDataByPost(String url, String jsonData) throws Exception {
String result = "";
PlatFormResponse platFormResponse = new PlatFormResponse();
if (client == null){
client = HttpClients.createDefault();
}
try {
HttpPost post = new HttpPost(url);
post.setHeader("Content-Type", "application/json");
StringEntity entity1 = new StringEntity(jsonData,"UTF-8");
post.setEntity(entity1);
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
platFormResponse.setFlag(PlatFormResponseConstant.ResponseBodyMsg.fail_ResultCode);
platFormResponse.setMsg(e.getMessage());
result = JSON.toJSONString(platFormResponse);
}
return result;
}
}