httpclient 3.0 테스트 코드

web 2007. 10. 24. 13:15

import org.apache.commons.httpclient.*;

import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.cookie.*;

public class Test {

    public Test() {
    }

   public void test(){
       
       String HOST = "i61959";

        HttpState initialState = new HttpState();
        String domain = "monitor.google.com";
         String path = "/";
        Cookie mycookie = new Cookie(domain, "JSESSIONID", "E52715F1C816B6CF556486A6", path, null, false);
        initialState.addCookie(mycookie);

 

        mycookie = new Cookie(domain, "kac", "1", path, null, false);
        initialState.addCookie(mycookie);


        mycookie = new Cookie(domain, "cnet_7", "1", path, null, false);
        initialState.addCookie(mycookie);

 

        HttpClient client = new HttpClient();
        //HttpMethod method = new GetMethod("http://i61974.google.com:80/common/monitor.gle?farm=vcomp");
        HttpMethod method = new GetMethod("http://m.google.com/anitor/requestbridge.gle?http://"+ HOST +".google.com:8080/common/monitor.gle?m=serverStatus");
        int statusCode = 0;
 client.setState(initialState);
 client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
        try {
            statusCode = client.executeMethod(method);
           
            byte[] responseBody = method.getResponseBody();
            System.out.println(new String(responseBody));

     // Get all the cookies
     Cookie[] cookies = client.getState().getCookies();
     // Display the cookies
     System.out.println("Present cookies: ");
     for (int i = 0; i < cookies.length; i++) {
  System.out.println(" - " + cookies[i].toExternalForm());
     }

        } catch (Exception e) {
            System.out.println("IOException");
            e.printStackTrace();
        }finally{
            method.releaseConnection();
        }      
    }

    public static void main(String[] args) {
              new Test().test();
    }
}

 

컴파일

 javac -classpath http.jar:.:logging.jar:codec.jar Test.java

//아파치 로깅과 codec 패키지에 대한 dependency가 있다.

 

 

실행

java -classpath http.jar:.:logging.jar:codec.jar Test

Posted by '김용환'
,