Home  >  Core Java

Example of CookieManager in Java

By Arvind Rai, April 06, 2013
CookieManager belongs to the package java.net. CookieManager is available since jdk1.6. CookieManager is the complete implementation of java.net.CookieHandler. CookieManager provides the getCookieStore() method to get information of stored cookie and getHost() returns the list of URI associated with the CookieManager.
CookieManagerTest.java
package com.concretepage;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
public class CookieManagerTest {
  public static void main(String args[]) throws Exception {
    String urlString = "http://www.concretepage.com";
    CookieManager ckman = new CookieManager();
    CookieHandler.setDefault(ckman);
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();
    connection.getContent();
    CookieStore ckStore = ckman.getCookieStore();
    List<URI> uriList=ckStore.getURIs();
    for (URI uri : uriList) {
        System.out.println(uri.getHost());
    }
    List<HttpCookie> cks = ckStore.getCookies();
    for (HttpCookie ck : cks) {
      System.out.println(ck);
    }
  }
}
Output
www.concretepage.com
PHPSESSID=fe688c9572eead2dbc69d833239644f9
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us