코드 사본은 다음과 같습니다.
import java.io.bufferedReader;
import java.io.ioexception;
import java.io.inputstream;
import java.io.inputStreamReader;
import org.apache.commons.httpclient.defaulthtpmethodretryhandler;
import org.apache.commons.httpclient.header;
import org.apache.commons.httpclient.httpclient;
import org.apache.commons.httpclient.httpexception;
import org.apache.commons.httpclient.httpstatus;
import org.apache.commons.httpclient.namevaluepair;
import org.apache.commons.httpclient.cookie.cookiepolicy;
import org.apache.commons.httpclient.methods.getMethod;
import org.apache.commons.httpclient.methods.postmethod;
import org.apache.commons.httpclient.params.httpmethodparams;
공개 클래스 testhttpclient {
public static void main (String [] args) {
// TODO 자동 생성 메소드 스텁
// httpclient의 인스턴스를 정의합니다
httpclient httppclient = new httpclient ();
// get 메소드 인스턴스를 만듭니다
getMethod getMethod = new getMethod ( "http://vevb.com");
// 시스템에서 제공 한 기본 복구 정책을 사용하십시오
//getMethod.getParams().SetParameter(httpmethodparams.retry_handler, new defaulthtpmethodretryhandler ());
// 사후 메소드 인스턴스를 만듭니다
postMethod postMethod = new UTF8PostMethod ( "http://vevb.com");
//
//// 각 양식 필드의 값을 채우십시오
// nameValuePair [] data = {new NameValuePair ( "user_name", "user_name"), new NameValuePair ( "password", "password")};
//
//// 양식의 값을 게시물 메소드에 넣습니다
//postmethod.setRequestBody(Data);
//
//postmethod.getParams (). setParameter (
//"http.protocol.cookie-policy",cookiepolicy.browser_compatibility);
//postmethod.setRequestheader("referer ","http://vevb.com ");
노력하다{
// get 메소드를 실행합니다
// int statusCode = httpclient.executemethod (getMethod);
// 게시물 메소드를 실행합니다
int statusCode = httpclient.executemethod (postmethod);
if (statusCode == httpstatus.sc_moved_temporally) {
헤더 LocationHeader = postMethod.getResponseHeader ( "위치");
문자열 위치 = null;
if (LocationHeader! = null) {
location = locationheader.getValue ();
}
postMethod = 새로운 postMethod (위치);
postMethod.setRequestHeader ( "참조", "http://vevb.com/login");
namevaluepair [] data1 = {new NamevaluePair ( "user_name", "user_name"), new NameValuePair ( "password", "password")};
postMethod.setRequestBody (data1);
postMethod.getParams (). setParameter (
"http.protocol.cookie-policy", cookiepolicy.browser_compatibility);
int stertatsCode1 = httpclient.executemethod (postmethod);
if (statusCode1! = httpstatus.sc_ok) {
System.out.println ( "메소드가 잘못되었습니다" + postMethod.getStatusline ());
}
}
if (statusCode! = httpstatus.sc_ok) {
System.out.println ( "메소드가 잘못되었습니다" + postMethod.getStatusline ());
}
inputStream responsebody = postMethod.getResponseBodyAsStream ();
bufferedReader reader = new bufferedReader (new inputStreamReader (응답 바디, "UTF-8"));
String line = reader.readline ();
while (line! = null) {
System.out.println (새 문자열 (line.getBytes ());
line = reader.readline ();
}
}
캐치 (httpexception e) {
// TODO : 예외를 처리합니다
System.out.println ( "제공된 HTTP 주소를 확인하십시오!");
e.printstacktrace ();
} catch (ioexception e) {
// TODO : 예외를 처리합니다
System.out.println ( "선이 잘못되었습니다!");
e.printstacktrace ();
} 마지막으로{
getMethod.releaseConnection (); // 링크를 릴리스합니다
postMethod.releaseConnection ();
}
}
// UTF-8 지원을위한 내부 클래스
public static class utf8postmethod는 postmethod {
public utf8postmethod (문자열 URL) {
슈퍼 (URL);
}
@보수
공개 문자열 getRequestcharset () {
// return super.getRequestCharSet ();
"UTF-8"리턴;
}
}
}