此方法只支持HTTP请求,HTTPS请求会报错!
示例如下:
/**
* 向指定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;
}
Original: https://www.cnblogs.com/xiaolibiji/p/15783123.html
Author: xiaolifc
Title: 向指定URL发送POST请求的方法
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/573784/
转载文章受原作者版权保护。转载请注明原作者出处!