Java API Tutorial - Java CookieStore.get(URI uri)








Syntax

CookieStore.get(URI uri) has the following syntax.

List < HttpCookie > get(URI uri)

Example

In the following code shows how to use CookieStore.get(URI uri) method.

import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.URI;
import java.net.URL;
import java.util.List;
/* w  w  w .  j  a  va2s .  co  m*/
public class Main {
  public static void main(String[] args) throws Exception {
    CookieManager cm = new CookieManager();
    cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cm);

    new URL("http://google.com").openConnection().getContent();
    CookieStore cookieStore = cm.getCookieStore();
    
    List<HttpCookie> cookies = cookieStore.get(new URI("http://google.com"));
    for (HttpCookie cookie : cookies) {
      System.out.println("Name = " + cookie.getName());
      System.out.println("Value = " + cookie.getValue());
      System.out.println("Lifetime (seconds) = " + cookie.getMaxAge());
      System.out.println("Path = " + cookie.getPath());
      System.out.println();
    }
  }
}