티스토리 뷰
GET 방식 요청
try{
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLContext(sslcontext.build())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();
String url = "https://..."; //요청할 주소
HttpGet httpGet = new HttpPost(url);
url+="?param1=data1&pram2=data2..."; // 넘길 데이터 예제
CloseableHttpResponse response = httpClient.execute(httpGet);
response.close();
httpClient.close();
}catch (Exception e){
e.printStackTrace();
}
POST 방식 요청
try{
SSLContextBuilder sslcontext = new SSLContextBuilder(); //https 의 경우
sslcontext.loadTrustMaterial(null, new TrustSelfSignedStrategy());
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLContext(sslcontext.build())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();
String url = "https://..."; //요청할 주소
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json");
//넘길 데이터 예제
//방식1
String data = "{'param1':'data1', 'param':'data2'}";
//방식2
//HashMap<String, Object> data = new HashMap<String, Object>();
//data.put("param1", "data1");
//방식3
//ExampleVo data = new ExampleVo();
//data.setParam1("data1");
ObjectMapper mapper = new ObjectMapper();
String jsonStr = mapper.writeValueAsString(data);
StringEntity strEnt = new StringEntity(jsonStr);
httpPost.setEntity(strEnt);
CloseableHttpResponse response = httpClient.execute(httpPost);
response.close();
httpClient.close();
}catch (Exception e){
e.printStackTrace();
}
'Language > JAVA' 카테고리의 다른 글
Spring boot 기본 템플릿부터 DB 연결까지 (0) | 2023.02.13 |
---|---|
lombok 설치했는데 @Data가 getter, setter 못 잡을 때 (0) | 2023.02.08 |
스프링(Spring) 500 에러 뜰 때 (0) | 2019.03.08 |
스프링(Spring) 프로젝트 알 수 없는 빨간 느낌표 (0) | 2019.03.08 |
댓글
공지사항
최근에 올라온 글