Fetch Cookie with CookieHandler, CookieManager and CookieStore : Cookie « Network « Java Tutorial






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 FetchCookie {
  public static void main(String args[]) throws Exception {
    String urlString = "http://java.sun.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);
    }
  }
}








19.23.Cookie
19.23.1.CookieStore
19.23.2.Fetch Cookie with CookieHandler, CookieManager and CookieStore
19.23.3.Using CookieHandler in Java 5