java.net.CookieStore Class in Java
Last Updated :
15 Dec, 2021
A CookieStore is an interface in Java that is a storage area for cookies. It is used to store and retrieve cookies. A CookieStore is responsible for removing HTTPCookie instances that have expired.
The CookieManager adds the cookies to the CookieStore for every incoming HTTP response by calling CookieStore.add() and retrieves the cookies from the CookieStore for every outgoing HTTP request by calling CookieStore.get().
The methods inside the CookieStore interface are shown in the table below.
Method | Action Performed |
---|
add(URI uri, HttpCookie cookie) | Adds one HTTP cookie to the store. |
get(URI uri) | Retrieves cookies whose domain matches the URI. |
getCookies() | Get all cookies in CookieStore which are not expired. |
getURIs() | Get all URIs that identify cookies in CookieStore |
remove(URI uri, HttpCookie cookie) | Removes a cookie from CookieStore |
removeAll() | Removes all cookies in the CookieStore |
Method 1: add(URI uri, HttpCookie cookie)
The add(URI uri, HttpCookie cookie) method adds one HTTP cookie to the store. This method is called for every incoming HTTP response. If a cookie corresponding to the given URI already exists, it is replaced with the new one.
Syntax:
public void add(URI uri, HttpCookie cookie)
Method 2: get(URI uri)
The get(URI uri) method retrieves cookies whose domain matches the URI. This method is called for every outgoing HTTP request. It returns an immutable list of HTTP cookies that are not expired. If no cookie matches the URI, it returns an empty list. If the parameter URI is passed as null, then it throws an exception called Null Pointer Exception.
Syntax:
public List<HttpCookie> get(URI uri)
Method 3: getCookies(
The getCookies() method retrieves all the cookies from the CookieStore, which are not expired. It returns an immutable list of HTTP cookies. If there is no cookie in the CookieStore, it returns an empty list.
Syntax:
public List<HttpCookie> getCookies()
Method 4: getURIs()
The getURIs() method retrieves all the URIs which identify the cookies in the CookieStore and returns an immutable list of URIs. It returns an empty list if no cookie in the CookieStore is associated with the URI.
Syntax:
public List<URI uri> getURIs()
Method 5: remove(URI uri, HttpCookie cookie)
The remove(URI uri, HttpCookie cookie) method removes a cookie in the CookieStore whose URI is associated with the given one. If the cookie to be removed is not related to the URI when added, then URI is passed as null. If the cookie is passed as null, it throws a Null Pointer Exception. If the cookie is successfully removed, then it returns a Boolean value true.
Syntax:
public boolean remove(URI uri, HttpCookie cookie)
Method 6: removeAll()
The removeAll() method removes all the cookies in the CookieStore. If all the cookies are successfully removed, then it returns a Boolean value true.
Syntax:
public boolean removeAll()
Program:
Java
import java.io.*;
import java.net.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// CookieManager and CookieStore
CookieManager cookieManager = new CookieManager();
CookieStore cookieStore
= cookieManager.getCookieStore();
// creating cookies and URI
HttpCookie cookieA = new HttpCookie("First", "1");
HttpCookie cookieB = new HttpCookie("Second", "2");
URI uri
= URI.create("https://www.geeksforgeeks.org/");
// Method 1 - add(URI uri, HttpCookie cookie)
cookieStore.add(uri, cookieA);
cookieStore.add(null, cookieB);
System.out.println(
"Cookies successfully added\n");
// Method 2 - get(URI uri)
List cookiesWithURI = cookieStore.get(uri);
System.out.println(
"Cookies associated with URI in CookieStore : "
+ cookiesWithURI + "\n");
// Method 3 - getCookies()
List cookieList = cookieStore.getCookies();
System.out.println("Cookies in CookieStore : "
+ cookieList + "\n");
// Method 4 - getURIs()
List uriList = cookieStore.getURIs();
System.out.println("URIs in CookieStore" + uriList
+ "\n");
// Method 5 - remove(URI uri, HttpCookie cookie)
System.out.println(
"Removal of Cookie : "
+ cookieStore.remove(uri, cookieA));
List remainingCookieList = cookieStore.getCookies();
System.out.println(
"Remaining Cookies : " + cookieList + "\n");
// Method 6 - removeAll()
System.out.println("Removal of all Cookies : "
+ cookieStore.removeAll());
List EmptyCookieList = cookieStore.getCookies();
System.out.println(
"Empty CookieStore : " + cookieList);
}
}
OutputCookies successfully added
Cookies associated with URI in CookieStore : [First="1"]
Cookies in CookieStore : [First="1", Second="2"]
URIs in CookieStore[https://www.geeksforgeeks.org]
Removal of Cookie : true
Remaining Cookies : [Second="2"]
Removal of all Cookies : true
Empty CookieStore : []
Similar Reads
java.net.CookiePolicy Class in Java
CookiePolicy implementations decide which cookies should be accepted and which should be rejected. Three pre-defined policy implementations are provided, namely ACCEPT_ALL, ACCEPT_NONE, and ACCEPT_ORIGINAL_SERVER. Signaturepublic interface CookiePolicyFields S.NO Field Description Data Type 1.ACCEPT
2 min read
java.net.CookieHandler Class in Java
The object of the CookieHandler Class in Java provides a callback mechanism for hooking up an HTTP state management policy implementation into the HTTP protocol handler. The mechanism of how to make HTTP requests and responses is specified by the HTTP state management mechanism. A system-wide Cookie
2 min read
java.net.CookieManager Class in Java
The CookieManager class provides a precise implementation of CookieHandler. This separates the storage of cookies from the policy surrounding accepting and rejecting cookies. A CookieManager is initialized with a CookieStore and a CookiePolicy. The CookieStore manages storage, and the CookiePolicy o
4 min read
Javax.servlet.http.Cookie class in Java
Many websites use small strings of text known as cookies to store persistent client-side state between connections. Cookies are passed from server to client and back again in the HTTP headers of requests and responses. Cookies can be used by a server to indicate session IDs, shopping cart contents,
7 min read
java.nio.file.FileStore Class in Java
Java.nio.file is a package in java that consists of the FileStore class. FileStore class is a class that provides methods for the purpose of performing some operations on file store. FileStore is a class that extends Object from java.lang package. And few methods the FileStore class can inherit from
4 min read
Java.net.HttpURLConnection Class in Java
HttpURLConnection class is an abstract class directly extending from URLConnection class. It includes all the functionality of its parent class with additional HTTP-specific features. HttpsURLConnection is another class that is used for the more secured HTTPS protocol. It is one of the popular choi
5 min read
java.net.URL Class in Java
URL is an acronym of Uniform resource locator. It is a pointer to locate resource in www (World Wide Web). A resource can be anything from a simple text file to any other like images, file directory etc. The typical URL may look like http://www.example.com:80/index.htmlThe URL has the following part
4 min read
java.nio.file.FileSystem class in java
java.nio.file.FileSystem class provides an interface to a file system. The file system acts as a factory for creating different objects like Path, PathMatcher, UserPrincipalLookupService, and WatchService. This object help to access the files and other objects in the file system. Syntax: Class decla
4 min read
Java.net.JarURLConnection class in Java
Prerequisite - JAR files in Java What is a Jar file? JavaArchive(JAR) bundles all the classes in one package. Since the archive is compressed and can be downloaded in a single HTTP connection, it is often faster to download the archive than to download individual classes. Although jar bundles all th
4 min read
java.net.Proxy Class in Java
A proxy is an immutable object and type of tool or application or program or system, which helps to protect the information of its users and computers. It acts as a barrier between computer and internet users. A Proxy Object defines the Proxy settings to be used with a connection. Proxy servers are
3 min read