Get Cookie value and set cookie value : Http Client « Apache Common « Java






Get Cookie value and set cookie value

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

public class GetCookiePrintAndSetValue {

  public static void main(String args[]) throws Exception {

    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "My Browser");

    GetMethod method = new GetMethod("http://localhost:8080/");
    try{
      client.executeMethod(method);
      Cookie[] cookies = client.getState().getCookies();
      for (int i = 0; i < cookies.length; i++) {
        Cookie cookie = cookies[i];
        System.err.println(
          "Cookie: " + cookie.getName() +
          ", Value: " + cookie.getValue() +
          ", IsPersistent?: " + cookie.isPersistent() +
          ", Expiry Date: " + cookie.getExpiryDate() +
          ", Comment: " + cookie.getComment());

        cookie.setValue("My own value");
      }
      client.executeMethod(method);
    } catch(Exception e) {
      System.err.println(e);
    } finally {
      method.releaseConnection();
    }
  }
}
           
       








GetCookiePrintAndSetValue.zip( 328 k)

Related examples in the same category

1.Get Http methods
2.Get Http client parameters
3.Execute Http method (post/get)
4.Http Client Simple Demo
5.Get allowed http methods
6.Http post method Example
7.Connect Method Example For Proxy Client
8.Basic Authentication Execute JSP Method
9.Basic Authentication For JSP Page
10.Basic Authentication Get JSP Method Return Code
11.Using Http Client Inside Thread