JAVA
[JAVA] CloseableHttpClient를 이용한 Http Post 호출 예제
Hwoarang757
2023. 7. 5. 23:08
CloseableHttpClient 이용 Http Post 호출 예제입니다.
/**
*
* @param jsonString
* @param urlAddress
* @return
*/
private static boolean httpClient(String jsonString,String urlAddress) {
String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
try {
HttpPost httpPost = new HttpPost(urlAddress);
httpPost.setHeader("Content-Type" , "application/x-www-form-urlencoded;charset=UTF-8");
List<NameValuePair> parameter = new ArrayList<>();
parameter.add(new BasicNameValuePair("parameter", jsonString ));
HttpEntity postEntity = new UrlEncodedFormEntity(parameter , "UTF-8");
httpPost.setEntity(postEntity);
try(CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
httpClient.execute(httpPost);
}
} catch(Exception exception) {
System.out.println(String.format("%s %s", methodName,exception));
return false;
}
return true;
}