카테고리 없음 2009. 7. 28. 14:14
package test;
import java.io.FileOutputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
// Get Method를 사용한 예제
public class Imdb {
 private static String url = "http://www.imdb.com/title/tt0120338/";
 private static StringBuffer sb = new StringBuffer();
 
 /**
  * @param args
  */
 public static void main(String[] args) {
System.out.println("main Strat  1");
  // HttpClient 생성
  HttpClient client = new HttpClient();
 
  // 요청 Method 지정
  HttpMethod method = new GetMethod(url);
  System.out.println("main Strat  2");
 
  try {
  
   // QueryString 지정, 1.문자열
//  url = url + "?mode=LSD&section_id=001&menu_id=001&view=1";
   // QueryString 지정, 2.NameValuePair 사용
//   NameValuePair nvp1= new NameValuePair("mode","LSD");
//   NameValuePair nvp2= new NameValuePair("section_id","001");
//   NameValuePair nvp3= new NameValuePair("menu_id","001");
//   NameValuePair nvp4= new NameValuePair("view","1");
//   method.setQueryString(new NameValuePair[]{nvp1,nvp2, nvp3, nvp4});
//   System.out.println("QueryString>>> "+method.getQueryString());
   // HTTP 요청 및 요청 결과
   int statusCode = client.executeMethod(method);
  System.out.println("#############   statusCode " + statusCode);
  System.out.println("#############   method " + method.getName());
   // 요청 결과..
   if (statusCode == HttpStatus.SC_OK) {
    System.out.println("요청 성공");
//    System.out.println("응답 HTML:\n" + method.getResponseBodyAsString());
  
    // 결과 저장
    Imdb.saveBytes("naver_news.html", method.getResponseBody());
   } else if ((statusCode == HttpStatus.SC_MOVED_TEMPORARILY) ||
                 (statusCode == HttpStatus.SC_MOVED_PERMANENTLY) ||
                 (statusCode == HttpStatus.SC_SEE_OTHER) ||
                 (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
    System.out.println("Redirecte !!!");
    System.out.println("Redirect target: " + method.getResponseHeader("location").getValue());
         }
  
  } catch (Exception e) {
   System.out.println("Exception 발생 ");
   e.printStackTrace();
  
   // Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
   // commons-logging.jar, commons-logging-api.jar 를 CLASSPATH에 추가한다.
  
   // Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/codec/DecoderException
   // commons-codec-1.3.jar 를 CLASSPATH에 추가한다.
  }
  System.out.println();
 }
 // 가져올 페이지 주소, 네이버 뉴스 속보
 
 
 // 파일에 byte[] 저장
 public static void saveBytes(String filepath, byte[] byteData) throws Exception {
  System.out.println("#############   saveBytes ");
  FileOutputStream foStream = null;
  try {
   sb.append(byteData);
   foStream = new FileOutputStream(filepath);
//   foStream.write(byteData);
  } finally {
   try { foStream.close(); foStream = null; } catch (Exception e) {
    System.out.println("saveBytes 익셉션 발생 ");
   }
  }
 }
commons-codec-1.3.jarcommons-httpclient-3.1.jarcommons-logging-1.1.1.jarcos.jarhttpclient-4.0-beta2.jarhttpcore-4.0.1.jarhttpcore-nio-4.0.1.jarhttpmime-4.0-beta2.jaribatis-2.3.4.726.jarlog4j-1.2.15.jar

 
posted by 나는너의힘
: