public static String httpPostWithJSON(String url,Map<String, String> params) throws Exception {
String json = JSON.toJSONString(params);
HttpPost httpPost = new HttpPost(url);
CloseableHttpClient client = HttpClients.createDefault();
String respContent = null;
// json方式
StringEntity entity = new StringEntity(json,“utf-8”);//解决中文乱码问题
entity.setContentEncoding(“UTF-8”);
entity.setContentType(“application/json”);
/httpPost.setEntity(entity);/
System.out.println();
//表单方式
List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
String temp;
for(Iterator<String> it = params.keySet().iterator(); it.hasNext();){
temp = it.next().toString();
pairList.add(new BasicNameValuePair(temp, params.get(temp)));
}
/*httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));*/
HttpResponse resp = client.execute(httpPost);
if(resp.getStatusLine().getStatusCode() == 200) {
HttpEntity he = resp.getEntity();
respContent = EntityUtils.toString(he,"UTF-8");
}
return respContent;
}