개발자의 길

[java] url 접속하여 html 가져오기 본문

4. JAVA

[java] url 접속하여 html 가져오기

자르르 2011. 9. 29. 18:19


1번째 방법

HttpURLConnection httpConn = null;  
String urlParameters=""; //파라메타값
Sring targetURL=http://test.com;
URL url = new URL(targetURL);
    httpConn = (HttpURLConnection)url.openConnection();
    
    //헤더 선언
    httpConn.setRequestMethod("POST");
    httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
    httpConn.setRequestProperty("Cookie", "cookievalue="+ cookie);
    
    httpConn.setUseCaches (false);
    httpConn.setDoInput(true);
    httpConn.setDoOutput(true);    

    PrintWriter pw = new PrintWriter(new OutputStreamWriter(httpConn.getOutputStream(), "utf-8"));
    pw.write(urlParameters);
    pw.flush();
    pw.close();
    

    //Get Response
    InputStream is = httpConn.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    String line;
    
    while((line = rd.readLine()) != null) {
     out.println(line);
     //response1.append('\r');
    }
 -----------------------------------------------------------------------------------------------------------

2번째 방법

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

public class test {
 public static void main(String[] args) {
  URL url         = null;
  StringBuffer sbHtml     = new StringBuffer();
  InputStream is       = null;
  BufferedReader br      = null;
  String data        = null;
  HttpURLConnection hurlc   = null;
  String keyword       = "강남구청";
  URLConnection urlc     = null;
  
  try{
   String strKeyword = URLEncoder.encode(keyword,"UTF-8");
   String strUrl   = "http://search.naver.com/search.naver?sm=tab_hty&where=nexearch&query="+strKeyword;
   // URL을 표시하는 String으로 URL클래스 객체생성.
   url = new URL(strUrl);
   urlc = (HttpURLConnection)url.openConnection();
   urlc.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2)");
   urlc.setRequestProperty("Accept-Language", "UTF-8");
   urlc.setRequestProperty("Method", "GET");
   urlc.setRequestProperty("Accept", "image/gif, image/xxbitmap, image/jpeg, image/pjpeg,application/xshockwaveflash, application/vnd.msexcel,application/vnd.mspowerpoint, application/msword, */*");
   urlc.setRequestProperty("Accept-Charset","UTF-8");
   urlc.setRequestProperty("Referer","http://www.naver.com/");
   urlc.setConnectTimeout(30000);
   urlc.setReadTimeout(30000);
   
   
   //입력 스트림을 이용하여 BufferedReader 클래스 객체를 생성.
   br = new BufferedReader(new InputStreamReader(urlc.getInputStream(), "UTF-8"));
   
   //입력 스트림에서 데이터가 완료될때까지 데이터를 읽어서 화면에 표시.
   while((data = br.readLine()) != null ){
    sbHtml.append(data);
   }
  
  }catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }
 }
 public static String removeHtml(String str){
  if(str==null)return "";
  return str.replaceAll("<(/)?([a-zA-Z]*)(\\s[a-zA-Z]*=[^>]*)?(\\s)*(/)?>", "");
 }
}
 
 



이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.
공유하기 링크
Comments