从大佬那里学到的高级写法,高级是之于我而言。留下demo方便自用。
public static void main(String[] args) {
String url = "";
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
/** HttpPost */
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> params = new ArrayList<NameValuePair>();
try {
params.add(new BasicNameValuePair("postDept", "testdept"));
params.add(new BasicNameValuePair("postTime", String.valueOf(System.currentTimeMillis())));
JSONObject json = new JSONObject();
json.put("documentTitle", "ucap测试0629");
json.put("releaseDate", "2020年06月24日");
json.put("pulishDate", "2020年06月24日");
json.put("year", "2020");
params.add(new BasicNameValuePair("postData", json));
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
/** HttpResponse */
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity, "utf-8");
//释放关闭所有资源链接
EntityUtils.consume(httpEntity);
} catch (Exception e) {
log.info("## release resouce error ##" + e);
}
}