써먹는 웹개발

[Java] 리눅스 curl 명령어를 Java 소스로 변경 본문

웹개발/Java & Jsp

[Java] 리눅스 curl 명령어를 Java 소스로 변경

kmhan 2023. 11. 17. 17:19


728x90
반응형

1. curl 명령어

1
curl -H "X-GPTWR-Authorization:C654321" -k -X POST -d "userId=test&otp=123456" https://192.168.100.999:8443/webapi/ver3/test-otp
cs

 

2. Java 소스

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
                CloseableHttpClient httpclient = createAcceptSelfSignedCertificateClient();
                
                // URL 설정
                String url = "https://192.168.100.999:8443/webapi/ver3/test-otp";
                HttpPost httpPost = new HttpPost(url); 
                httpPost.addHeader("X-GPTWR-Authorization""C654321");
 
                List<NameValuePair> postParams = new ArrayList<NameValuePair>();
                postParams.add(new BasicNameValuePair("userId""test"));
                postParams.add(new BasicNameValuePair("otp""123456"));
 
                HttpEntity postEntity = new UrlEncodedFormEntity(postParams);
                httpPost.setEntity(postEntity);
                
                CloseableHttpResponse clientResponse = httpclient.execute(httpPost);
                String json = EntityUtils.toString(clientResponse.getEntity(), "UTF-8");
                
                httpclient.close();
                clientResponse.close();
                Map<StringString> map = new HashMap<StringString>();
                if(StringUtils.isNotBlank(json)) {
                    ObjectMapper mapper = new ObjectMapper();
                    map = mapper.readValue(json, new TypeReference<Map<StringString>>(){});
                    if("0000".equals(map.get("result"))) {
                        result.setSucess("성공");
                    } else {
                        result.setProg("실패");
                    }
                }
cs
728x90
반응형


Comments