카테고리 없음
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;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
// Get Method를 사용한 예제
public class Imdb {
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();
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§ion_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});
try {
// QueryString 지정, 1.문자열
// url = url + "?mode=LSD§ion_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());
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.jar
commons-httpclient-3.1.jar
commons-logging-1.1.1.jar
cos.jar
httpclient-4.0-beta2.jar
httpcore-4.0.1.jar
httpcore-nio-4.0.1.jar
httpmime-4.0-beta2.jar
ibatis-2.3.4.726.jar
log4j-1.2.15.jar
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 익셉션 발생 ");
}
}
}
}
![](https://i1.daumcdn.net/cfs.tistory/v/0/blog/image/extension/unknown.gif)
![](https://i1.daumcdn.net/cfs.tistory/v/0/blog/image/extension/unknown.gif)
![](https://i1.daumcdn.net/cfs.tistory/v/0/blog/image/extension/unknown.gif)
![](https://i1.daumcdn.net/cfs.tistory/v/0/blog/image/extension/unknown.gif)
![](https://i1.daumcdn.net/cfs.tistory/v/0/blog/image/extension/unknown.gif)
![](https://i1.daumcdn.net/cfs.tistory/v/0/blog/image/extension/unknown.gif)
![](https://i1.daumcdn.net/cfs.tistory/v/0/blog/image/extension/unknown.gif)
![](https://i1.daumcdn.net/cfs.tistory/v/0/blog/image/extension/unknown.gif)
![](https://i1.daumcdn.net/cfs.tistory/v/0/blog/image/extension/unknown.gif)
![](https://i1.daumcdn.net/cfs.tistory/v/0/blog/image/extension/unknown.gif)