Java中使用HttpPost方式調用接口的方法是:
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("param1", "value1"));
params.add(new BasicNameValuePair("param2", "value2"));
httpPost.setEntity(new UrlEncodedFormEntity(params));
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
String responseBody = EntityUtils.toString(entity);
// 處理響應數據
} finally {
response.close();
}
httpClient.close();
注意:以上代碼僅為示例,實際使用時需要根據具體情況進行修改。另外,上述方法是同步調用,如果需要異步調用可以使用Apache HttpAsyncClient或者使用Java的CompletableFuture等方式。