Java API Tutorial - Java CookieManager .getCookieStore ()








Syntax

CookieManager.getCookieStore() has the following syntax.

public CookieStore getCookieStore()

Example

In the following code shows how to use CookieManager.getCookieStore() method.

//from   w  w  w.  j  a  v  a2 s  . co  m

import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

public class Main {
  public static void main(String args[]) throws Exception {
    String urlString = "http://google.com";
    CookieManager manager = new CookieManager();
    CookieHandler.setDefault(manager);
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();
    Object obj = connection.getContent();
    url = new URL(urlString);
    connection = url.openConnection();
    obj = connection.getContent();
    CookieStore cookieJar = manager.getCookieStore();
    List<HttpCookie> cookies = cookieJar.getCookies();
    for (HttpCookie cookie : cookies) {
      System.out.println(cookie);
    }
  }
}

The code above generates the following result.