BLOG main image
분류 전체보기 (28)
루비 (3)
glassfish (1)
건아 (1)
RFC 한글 (0)
TIP (5)
VCS(Version Control System) (3)
perl (4)
java (2)
android (0)
javascript&Jquery (2)
기술문서 번역 (0)
사이베이스(ASE) (3)
용어 (2)
GAE&GCP (0)
사는 이야기 (1)
Visitors up to today!
Today hit, Yesterday hit
daisy rss
tistory 티스토리 가입하기!
'자바 데이타 전송'에 해당되는 글 1건
2014. 5. 29. 11:32

보통 HttpClient로 작업을 해 왔는데 이번에는 HttpUrlConnection으로 작업을 해야했다. 
(특정 호스팅 상황에서 HttpClient가 지원되지 않는 상황이 발생)

아래의 소스는 SMS 발송서버로 데이타를 던지는 샘플이다.

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
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
@SuppressWarnings("serial")
public class SENDSMSServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        String send_num="112";
        String receive_num="01012345678,01009876543";
        String contents = "다건 SMS 테스트중입니다. ";
        System.out.println("values " + values);
//        boolean sms = httpConnForSMS(values);
        boolean sms = httpConnForSMS(send_num,receive_num,contents);
        System.out.println("SEND RESULT " + sms);
    }
    
public static boolean httpConnForSMS(String sendNum, String receiveNum, String contents) {
        
        /**
         * params[0] : sender
         * params[1] : receiver
         * params[2] : contents
         */
        
        boolean result = false;
        try {
            URL url = new URL("http://xxxxxxxxx/_include/send_sms.php");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 서버로부터 메세지를 받을 수 있도록 설정. (기본값 : true)
            connection.setDoOutput(true);
            // 헤더 설정
            // 메소드 설정 (기본값 : GET)
            connection.setRequestMethod("POST");
            // 서버로 메세지를 보낼 수 있도록 설정.
            connection.setDoOutput(true);
                       
            // 파라미터 전송.
            StringBuilder sb = new StringBuilder();
            sb.append("send_num=")
              .append(URLEncoder.encode(sendNum, "UTF-8"))
              .append("&receive_nums=")
              .append(URLEncoder.encode(receiveNum, "UTF-8"))
              .append("&sms_con=")
              .append(URLEncoder.encode(contents, "UTF-8"));
            
            OutputStream writer = connection.getOutputStream();
            writer.write(sb.toString().getBytes("UTF-8"));
            writer.flush();
            writer.close();
            
            //////////////////////////////////////////////////
            ////////////////// If GET Method  ////////////////            
            //String urlStrings = "http://......";
            //urlStrings += "&sender=";
            //HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //////////////////////////////////////////////////
            int responseCode = connection.getResponseCode();   
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 발송성공..
                result = true;
            } else {
                // 발송실패..
            }
        } catch (MalformedURLException e) {
            // ...
            e.printStackTrace();
        } catch (IOException e) {
            // ...
            e.printStackTrace();
        } finally {
            
        }
 
        return result;
    }
}


'java' 카테고리의 다른 글

Mysql에서 Oracel로 쿼리 변경시 limit  (0) 2013.12.26
prev"" #1 next