示例如下:
/**
* 向指定URL发送POST请求,格式为JSON
* @param url
* @param jsonStr
* @return
* @throws Exception
*/
public static String sendHttpPost(String url, String jsonStr) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
httpPost.setEntity(new StringEntity(jsonStr));
CloseableHttpResponse response = httpClient.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode() + "\n");
HttpEntity entity = response.getEntity();
String responseContent = EntityUtils.toString(entity, "UTF-8");
System.out.println("loginResultJson:" + responseContent);
response.close();
httpClient.close();
return responseContent;
}