🖥Web/🔥Java

[JAVA] URL 코드보기

후눅스 2020. 3. 18. 12:21
반응형
반응형

 

package book.chap15;

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

public class ViewURL {
	
	public ViewURL() {}
	public ViewURL(String strURL) {
		URL myURL = null;
		URLConnection urlCon = null;
		InputStream is = null;
		BufferedReader br = null;
		String data = null;
		String headerType = null;
		try {
			myURL = new URL(strURL);
			urlCon = myURL.openConnection();
			urlCon.connect();
			headerType = urlCon.getContentType();//mime type
			is = urlCon.getInputStream();
			br = new BufferedReader(new InputStreamReader(is));
			while ((data = br.readLine())!=null) {
				System.out.println(data);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
//		new ViewURL("https://www.naver.com/");
//		new ViewURL("https://www.naver.com/index.jsp");
//		new ViewURL("https://www.naver.com/index.html");
//		new ViewURL("https://www.google.com");
//		new ViewURL("https://www.google.com/index.jsp");
//		new ViewURL("https://www.google.com/index.html");
//		new ViewURL("http://localhost:8000/index.jsp");
	}
}

 

 

위처럼 작성시 URL의 코드를 가져올 수 있다.

반응형