프로그래밍 언어 /java

cafe24 sms전송 api java 버젼으로 변경한 소스

dreammarker 2019. 2. 1. 11:59



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.metarights.fairwage.cmm.sms.service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import org.apache.log4j.Logger;
import com.metarights.fairwage.cmm.model.SmsObject;
 
 
public class SMSService {
 
    private final static String apiUrl        =    "https://sslsms.cafe24.com/sms_sender.php";
 
    private final static String userAgent    =    "Mozilla/5.0";
 
    private final static String charset        =    "UTF-8";
 
    private final static boolean isTest        =    true;
 
    
 
    private static final Logger logger =    Logger.getLogger(SMSService.class);
 
    public void sendSMSAsync(SmsObject smsobject){
 
        try{
  
            URL    obj    =new URL(apiUrl);
 
            HttpsURLConnection con= (HttpsURLConnection) obj.openConnection();
 
            con.setRequestProperty("Content-type""application/x-www-form-urlencoded");
 
            con.setRequestProperty("Accept-Charset", charset);
 
            con.setRequestMethod("POST");
 
            con.setRequestProperty("User-Agent", userAgent);
 
            
            String postParams = "user_id="+base64Encode("아이디")
 
                                +"&secure="+base64Encode("인증서암호")
 
                                +"&msg="+base64Encode("메세지내용")+"&sphone1="+base64Encode("발신자전화번호첫번째")+"&sphone2="+base64Encode("발신자전화번호두번째")+"&sphone3="+base64Encode("발신자전화번호세번째")
 
                                +"&mode="+base64Encode("1")+"&smsType=S"+"&rdate"+base64Encode("")+"&rtime"+base64Encode(""); // SMS/LMS 여부
 
        
            //test 모드일 경우 실제로 SMS발송은 되지 않고 성공적인 호출 확인 여부만 확인 할 수 있도록 함
 
            if(isTest) {
 
//                postParams+="&testflag"+base64Encode("Y");
 
            }
 
            
 
            //For POST only    - START
 
            con.setDoOutput(true);
 
            OutputStream os = con.getOutputStream();
 
            os.write(postParams.getBytes());
 
            os.flush();
 
            os.close();
 
            //For POST only - END
 
            int responseCode = con.getResponseCode();
 
            logger.info("POST Response Code::"+responseCode);
 
            
 
            if(responseCode == HttpURLConnection.HTTP_OK){ // success
 
                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
 
                String inputLine;
 
                StringBuffer buf  = new StringBuffer();
 
                
 
                while((inputLine=in.readLine())!=null){
 
                    buf.append(inputLine);
 
                }
 
                in.close();
 
                
 
                logger.info("SMS Content : "+buf.toString());
 
            }else{
 
                logger.error("POST request not worked");
 
            }
 
        }catch(IOException ex){
 
            logger.error("SMS IOException:"+ex.getMessage());
 
        }
 
    }
 
    public static String base64Encode(String str)  throws java.io.IOException {
 
        sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
 
        byte[] strByte = str.getBytes();
 
        String result = encoder.encode(strByte);
 
        return result ;
 
    }
 
 
 
    /**
     * BASE64 Decoder
     * @param str
     * @return
     */
 
    public static String base64Decode(String str)  throws java.io.IOException {
 
        sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
 
        byte[] strByte = decoder.decodeBuffer(str);
 
        String result = new String(strByte);
 
        return result ;
 
    }
 
}
 
 
 
 
 
 
 
cs