一、导入需要用到的maven包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.32</version>
</dependency>
二、java代码
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
JSONObject jsonObject = new JSONObject();
jsonObject.put("key", "hjkhkdsa");
try {
StringEntity stringEntity = new StringEntity((JSONObject.toJSONString(jsonObject)), "application/json", "utf-8");
httpPost.setEntity(stringEntity);
CloseableHttpResponse closeableHttpResponse =httpClient.execute(httpPost);
HttpEntity httpEntity = closeableHttpResponse.getEntity();
res = EntityUtils.toString(httpEntity);
} catch (Exception e) {
e.printStackTrace();
}