써먹는 웹개발

[솔루션] 다이렉트 센드(direct send) sms java api 안 먹는 현상 해결방법 본문

Client/기타 솔루션

[솔루션] 다이렉트 센드(direct send) sms java api 안 먹는 현상 해결방법

kmhan 2023. 3. 28. 11:28


728x90
반응형

1. 제공하는 API 소스

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
31
32
33
34
35
        String urlParameters = "title=" + java.net.URLEncoder.encode(org.apache.commons.codec.binary.Base64.encodeBase64String(title.getBytes("euc-kr")), "EUC_KR")
                + "&message=" + java.net.URLEncoder.encode(org.apache.commons.codec.binary.Base64.encodeBase64String(message.getBytes("euc-kr")), "EUC_KR")
                + "&sender=" + java.net.URLEncoder.encode(sender, "EUC_KR")
                + "&username=" + java.net.URLEncoder.encode(username, "EUC_KR")
                + "&recipients=" + java.net.URLEncoder.encode(recipients, "EUC_KR")
                //+ "&return_url=" + java.net.URLEncoder.encode(returnURL, "EUC_KR") // returnURL이 있는 경우 주석해제 바랍니다.
                + "&key=" + java.net.URLEncoder.encode(key, "EUC_KR");
                + "&type=" + java.net.URLEncoder.encode("java""EUC_KR");
                //+ "&attaches=" + java.net.URLEncoder.encode(attaches, "EUC_KR");    // 첨부파일이 있는 경우 주석해제 바랍니다.
        /** 수정하지 마시기 바랍니다. **/
 
        System.setProperty("jsse.enableSNIExtension""false") ; 
        con.setDoOutput(true);
        java.io.DataOutputStream wr = new java.io.DataOutputStream(con.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();
 
        int responseCode = con.getResponseCode();
        System.out.println(responseCode);
        
        /* 
         * responseCode 가 200 이 아니면 내부에서 문제가 발생한 케이스입니다. 
         * directsend 관리자에게 문의해주시기 바랍니다.
         * */
        
        java.io.BufferedReader in = new java.io.BufferedReader(
                new java.io.InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
 
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
cs

 

2. 해결방법

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
31
32
        String urlParameters = "title=" + java.net.URLEncoder.encode(org.apache.commons.codec.binary.Base64.encodeBase64String(title.getBytes("euc-kr")), "EUC_KR")
                + "&message=" + java.net.URLEncoder.encode(org.apache.commons.codec.binary.Base64.encodeBase64String(message.getBytes("euc-kr")), "EUC_KR")
                + "&sender=" + java.net.URLEncoder.encode(sender, "EUC_KR")
                + "&username=" + java.net.URLEncoder.encode(username, "EUC_KR")
                + "&recipients=" + java.net.URLEncoder.encode(recipients, "EUC_KR")
                //+ "&return_url=" + java.net.URLEncoder.encode(returnURL, "EUC_KR") // returnURL이 있는 경우 주석해제 바랍니다.
                + "&key=" + java.net.URLEncoder.encode(key, "EUC_KR");
                + "&type=" + java.net.URLEncoder.encode("java""EUC_KR");
                //+ "&attaches=" + java.net.URLEncoder.encode(attaches, "EUC_KR");    // 첨부파일이 있는 경우 주석해제 바랍니다.
        /** 수정하지 마시기 바랍니다. **/
 
                // 타임아웃 설정
                HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
                CloseableHttpClient httpClient = HttpClientBuilder.create()
                                                                .setMaxConnTotal(100)
                                                                .setMaxConnPerRoute(50)
                                                                .build();
                
                factory.setReadTimeout(5000); // 읽기 시간 초과 5초
                factory.setConnectTimeout(3000); // 연결 시간 초과 3초
                factory.setHttpClient(httpClient); // 연결 합계 100개까지 설정
                
                // RestTemplate 객체 생성
                RestTemplate restTemplate = new RestTemplate(factory);
                
                HttpHeaders headers = new HttpHeaders();
                Charset utf8 = Charset.forName("UTF-8");
                MediaType mediaType = new MediaType("application""json", utf8);
                headers.setContentType(mediaType);
 
                HttpEntity<String> entity = new HttpEntity<String>(urlParameters,headers);
                String answer = restTemplate.postForObject(url, entity, String.class);
cs

postForObject()는 getForObject()와 같이 POST 요청에 대해서 반환 값을 해당 객체로 반환해주는 메서드입니다.

 


231110 SMS 예제 소스

728x90
반응형


Comments