Java‎ > ‎

HTTP

GET Cookies
 
 
 public static void getCookie() {
  HttpClient client = new HttpClient();
  client.getParams().setParameter("http.useragent", "kr browser");
  BufferedReader br = null;
  PostMethod method = new PostMethod(
    "http://code.compute.info/login.do");
  method.addParameter("username", "admin");
  method.addParameter("password", "admin");
  method.addParameter("projectname", "PROJ1");
  method.addParameter("persistCookie", "on");
  try {
   int returnCode = client.executeMethod(method);
   // still consume the response body
   method.getResponseHeaders();
   for (int i = 0; i < client.getState().getCookies().length; i++) {
    Cookie cookie = client.getState().getCookies()[i];
    System.err.println("Cookie: " + cookie.getName() + ", Value: "
      + cookie.getValue() + ", IsPersistent?: "
      + cookie.isPersistent() + ", Expiry Date: "
      + cookie.getExpiryDate() + ", Comment: "
      + cookie.getComment());
   }
  } catch (Exception x) {
   x.printStackTrace();
  }
 } 
Comments