seong

URL 통신하기(naver.com 홈페이지) 본문

자바/자바 실습

URL 통신하기(naver.com 홈페이지)

hyeonseong 2022. 8. 22. 14:48

naver.com 홈페이지 내용 가져오기

package communication;

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

import javax.net.ssl.HttpsURLConnection;

public class NaverProgram {

	public static void main(String[] args) {

		try {
        	//url주소 
			URL url = new URL("https://www.naver.com"); 
            
            // url을 연결
			HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();  
            
            // 데이터를 BufferReader를 이용해 담아준다.
       
			BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			
			String temp = "";
            
            //  버퍼의 값을 readLine을 한 후 temp에 담아주고 null이 아니라면 출력
			while ((temp = br.readLine()) != null) {
				System.out.println(temp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

naver.com 데이터가 정상적으로 가져와졌다.