HTTP傳送POST請求
public void postSend(String reqJsonData, String documentCode, String userId) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
String requestUrl = ConfigurationCache.getInstance().getValue(ConfigurationCache.REQUEST_SERVICE_URL);
HttpPost httpPost = new HttpPost(requestUrl + "/testapi/test/send");
httpPost.setHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8.toString());
httpPost.setEntity(new StringEntity(reqJsonData, "UTF-8"));
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
int errorCode = response.getStatusLine().getStatusCode();
logger.info("請求響應的狀態:" + errorCode);
String responseResult = EntityUtils.toString(response.getEntity());
ResponseData responseData = XmlParseUtil.xmlToObj(responseResult, ResponseData.class, null);
logger.info("響應的結果:" + JSON.toJSONString(responseData));
response.close();
httpClient.close();
} catch (Exception e) {
logger.error("請求發生異常,原因:" , e);
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
}
}