Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 톰캣
- Docker Compose
- mysql
- catalina.out
- 만들면서 배우는 클린 아키텍처
- Entity Code 치환
- for문
- aws elasticache 활용
- select
- redis + spring boot 함께
- 자바
- docker 컨테이너로 띄우기
- js
- springboot+redis
- javascript
- 특수문자 치환
- Linux
- 초단위
- 자바스크립트
- 엔티티 코드 치환
- jQuery
- insert
- Tomcat
- architecture
- sftp
- Java
- Stack
- 리눅스
- 정규식
- 제이쿼리
Archives
- Today
- Total
꾸준하게, 차근차근
[Java] POST 데이터 전송(application/x-www-form-urlencoded) 구현 본문
1. application/json과 application/x-www-form-urlencoded
application/json은 {key: value}의 형태로 전송되지만 application/x-www-form-urlencoded는 key=value&key=value의 형태로 전달된다.
즉 application/x-www-form-urlencoded는 보내는 데이터를 URL인코딩 이라고 부르는 방식으로 인코딩 후에 웹서버로 보내는 방식을 의미한다.
public JSONObject httpPost(String url, HashMap<String, Object> param) {
JSONObject jsonObj = null;
try {
// TLS/SSL 통신 무시
TrustManager[] trustAllcerts = new TrustManager[] {
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; }
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException { }
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException { }
}
};
// SSL INIT
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllcerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){
public boolean verify(String string, SSLSession ssls){
return true;
}
});
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
for( String key : param.keySet() ) {
map.add(key, (String) param.get(key));
}
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(map, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestEntity, String.class);
JSONParser jsonParser = new JSONParser();
String getBody = responseEntity.getBody();
jsonObj = (JSONObject) jsonParser.parse(getBody);
} catch(Exception e) {
e.printStackTrace();
jsonObj = null;
}
} catch(Exception e) {
e.printStackTrace();
}
return jsonObj;
}
🙏 참조 ::
'Java' 카테고리의 다른 글
[Java] JsonString To Map & JsonString To List Map (0) | 2022.04.28 |
---|---|
[Java] Map - getOrDefault 메소드 (0) | 2022.04.26 |
[Java] Map To List (0) | 2022.04.26 |
[Java] List<Integer> To int[] (0) | 2022.04.26 |
[Java] SFTP 파일 전송(업로드) 구현(JSch 사용) (0) | 2022.04.26 |