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; } }