자바로 http 통신하는 샘플소스


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
 
public class HttpNetwork  {
 
    public static void main (String args[]) {
  
        HttpNetwork net = new HttpNetwork();
        String strHtmlSource = net.strGetData();
        System.out.println("※Recieve HTML ("+getCurrentDate24() +") \n"+strHtmlSource);
  
    }
 
    private String strGetData() {
  
        BufferedReader    oBufReader = null;
        HttpURLConnection httpConn   = null;
        String strBuffer = "";
        String strRslt   = "";
      
        try
        {
            String strEncodeUrl = "http://kyoboaxa.ktspeedway.co.kr/traf?refresh=15&unit=K";
            URL oOpenURL = new URL(strEncodeUrl);
          
            httpConn =  (HttpURLConnection) oOpenURL.openConnection();          
            httpConn.setRequestMethod("POST");          
            httpConn.connect();          
            oBufReader = new BufferedReader(new InputStreamReader(oOpenURL.openStream()));
  
            //Buffer에 있는 내용을 읽어 차례로 화면에 뿌려준다.
            while((strBuffer = oBufReader.readLine()) != null)
            {
                if(strBuffer.length() > 1)
                {
                    strRslt += strBuffer;
                }
            }
          
        } catch( Exception ee) {
          ee.getMessage();
        }
  
        return strRslt;
    }
 
    /**
     * <p>현재날짜시간가져오기</p>
     * @return
     */
    private static String getCurrentDate24()
    {
        java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.KOREA);
        return formatter.format(new java.util.Date());
    }
}
 



[원본자료] http://kcslancer.springnote.com/pages/5638031


'프로그램 > Java & Jsp' 카테고리의 다른 글

[LIB] MSSQL Server용 Microsoft JDBC Driver  (0) 2013.02.06
[log4j] log4j 설정 및 사용법  (1) 2013.01.10
[Java] Thread 강좌...  (0) 2012.10.05
[펌] 향상된 for문  (0) 2012.07.06
throw & throws in Java  (0) 2012.03.12

+ Recent posts