소프트웨어 개발/Java - Basic

자바(Java)에서 HTML파일 읽어오기 예제

늘근이 2014. 5. 11. 19:43

출처는 다음과 같다

http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256DC80053681B




package financialstatement_parser;

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

public class test {
	
	public static void main(String[] args) {
		
		String result = getHTML("http://daum.net");
		System.out.println(result);
		
		
		
	}//end main()

	
	 public static String getHTML(String urlToRead) {
	      URL url; // The URL to read
	      HttpURLConnection conn; // The actual connection to the web page
	      BufferedReader rd; // Used to read results from the web page
	      String line; // An individual line of the web page HTML
	      String result = ""; // A long string containing all the HTML
	      try {
	         url = new URL(urlToRead);
	         conn = (HttpURLConnection) url.openConnection();
	         conn.setRequestMethod("GET");
	         rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
	         while ((line = rd.readLine()) != null) {
	            result += line;
	         }
	         rd.close();
	      } catch (Exception e) {
	         e.printStackTrace();
	      }
	      return result;
	   }
	
	
}//end public class