티스토리 뷰

DeVeLoP

[JaVa] http 통신 예제 코드

freecatz 2019. 2. 22. 10:27

출처 : http://whitegom.tistory.com/26

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class httpGetXmlTest {

 public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
  
  URL url = new URL("http://192.168.137.130:8080/exam/xml3/admin");

  // open connection
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setDoInput(true);          // 입력스트림 사용
  conn.setDoOutput(true);         // 출력스트림 사용
  conn.setUseCaches(false);       // 캐시사용 안함
  conn.setReadTimeout(30000);     // 타임아웃 : 3초
  conn.setRequestMethod("GET");   // GET or POST ...
        
  StringBuffer sb =  new StringBuffer();

  BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

  for (;;) {
   String line = br.readLine();
   if (line == null)
    break;
   sb.append(line + "\n");
  }

  br.close();
  conn.disconnect();
        
  String getXml = sb.toString();

  System.out.println(sb.toString());        
 }
}


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/12   »
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
글 보관함