Java发送Http请求
public static String httpRequest(String url, String json) throws IOException {
String result = “”;
HttpPost httpPost = new HttpPost(url);
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
BasicResponseHandler handler = new BasicResponseHandler();
StringEntity entity = new StringEntity(json, “utf-8”);//解决中文乱码问题
entity.setContentEncoding(“UTF-8”);
entity.setContentType(“application/json”);
httpPost.setEntity(entity);
result = httpClient.execute(httpPost, handler);
System.out.println(“----===”+result);
return result;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}