Stay organized with collections
Save and categorize content based on your preferences.
CookieStore
public
interface
CookieStore
A CookieStore object represents a storage for cookie. Can store and retrieve
cookies.
CookieManager
will call CookieStore.add
to save cookies
for every incoming HTTP response, and call CookieStore.get
to
retrieve cookie for every outgoing HTTP request. A CookieStore
is responsible for removing HttpCookie instances which have expired.
Summary
Public methods |
abstract
void
|
add(URI uri, HttpCookie cookie)
Adds one HTTP cookie to the store.
|
abstract
List<HttpCookie>
|
get(URI uri)
Retrieve cookies associated with given URI, or whose domain matches the
given URI.
|
abstract
List<HttpCookie>
|
getCookies()
Get all not-expired cookies in cookie store.
|
abstract
List<URI>
|
getURIs()
Get all URIs which identify the cookies in this cookie store.
|
abstract
boolean
|
remove(URI uri, HttpCookie cookie)
Remove a cookie from store.
|
abstract
boolean
|
removeAll()
Remove all cookies in this cookie store.
|
Public methods
add
public abstract void add (URI uri,
HttpCookie cookie)
Adds one HTTP cookie to the store. This is called for every
incoming HTTP response.
A cookie to store may or may not be associated with an URI. If it
is not associated with an URI, the cookie's domain and path attribute
will indicate where it comes from. If it is associated with an URI and
its domain and path attribute are not specified, given URI will indicate
where this cookie comes from.
If a cookie corresponding to the given URI already exists,
then it is replaced with the new one.
Parameters |
uri |
URI : the uri this cookie associated with.
if null , this cookie will not be associated
with an URI |
cookie |
HttpCookie : the cookie to store |
get
public abstract List<HttpCookie> get (URI uri)
Retrieve cookies associated with given URI, or whose domain matches the
given URI. Only cookies that have not expired are returned.
This is called for every outgoing HTTP request.
Parameters |
uri |
URI : the uri associated with the cookies to be returned |
Returns |
List<HttpCookie> |
an immutable list of HttpCookie,
return empty list if no cookies match the given URI |
getCookies
public abstract List<HttpCookie> getCookies ()
Get all not-expired cookies in cookie store.
Returns |
List<HttpCookie> |
an immutable list of http cookies;
return empty list if there's no http cookie in store |
getURIs
public abstract List<URI> getURIs ()
Get all URIs which identify the cookies in this cookie store.
Returns |
List<URI> |
an immutable list of URIs;
return empty list if no cookie in this cookie store
is associated with an URI |
remove
public abstract boolean remove (URI uri,
HttpCookie cookie)
Remove a cookie from store.
Parameters |
uri |
URI : the uri this cookie associated with.
if null , the cookie to be removed is not associated
with an URI when added; if not null , the cookie
to be removed is associated with the given URI when added. |
cookie |
HttpCookie : the cookie to remove |
Returns |
boolean |
true if this store contained the specified cookie |
removeAll
public abstract boolean removeAll ()
Remove all cookies in this cookie store.
Returns |
boolean |
true if this store changed as a result of the call |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[null,null,["Last updated 2025-02-10 UTC."],[],[],null,["# CookieStore\n\nAdded in [API level 9](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\nCookieStore\n===========\n\n\n`\npublic\n\n\ninterface\nCookieStore\n`\n\n\n`\n\n\n`\n\n|----------------------|\n| java.net.CookieStore |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nA CookieStore object represents a storage for cookie. Can store and retrieve\ncookies.\n\n[CookieManager](/reference/java/net/CookieManager) will call `CookieStore.add` to save cookies\nfor every incoming HTTP response, and call `CookieStore.get` to\nretrieve cookie for every outgoing HTTP request. A CookieStore\nis responsible for removing HttpCookie instances which have expired.\n\nSummary\n-------\n\n| ### Public methods ||\n|-------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` abstract void` | ` `[add](/reference/java/net/CookieStore#add(java.net.URI,%20java.net.HttpCookie))`(`[URI](/reference/java/net/URI)` uri, `[HttpCookie](/reference/java/net/HttpCookie)` cookie) ` Adds one HTTP cookie to the store. |\n| ` abstract `[List](/reference/java/util/List)`\u003c`[HttpCookie](/reference/java/net/HttpCookie)`\u003e` | ` `[get](/reference/java/net/CookieStore#get(java.net.URI))`(`[URI](/reference/java/net/URI)` uri) ` Retrieve cookies associated with given URI, or whose domain matches the given URI. |\n| ` abstract `[List](/reference/java/util/List)`\u003c`[HttpCookie](/reference/java/net/HttpCookie)`\u003e` | ` `[getCookies](/reference/java/net/CookieStore#getCookies())`() ` Get all not-expired cookies in cookie store. |\n| ` abstract `[List](/reference/java/util/List)`\u003c`[URI](/reference/java/net/URI)`\u003e` | ` `[getURIs](/reference/java/net/CookieStore#getURIs())`() ` Get all URIs which identify the cookies in this cookie store. |\n| ` abstract boolean` | ` `[remove](/reference/java/net/CookieStore#remove(java.net.URI,%20java.net.HttpCookie))`(`[URI](/reference/java/net/URI)` uri, `[HttpCookie](/reference/java/net/HttpCookie)` cookie) ` Remove a cookie from store. |\n| ` abstract boolean` | ` `[removeAll](/reference/java/net/CookieStore#removeAll())`() ` Remove all cookies in this cookie store. |\n\nPublic methods\n--------------\n\n### add\n\nAdded in [API level 9](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract void add (URI uri, \n HttpCookie cookie)\n```\n\nAdds one HTTP cookie to the store. This is called for every\nincoming HTTP response.\n\nA cookie to store may or may not be associated with an URI. If it\nis not associated with an URI, the cookie's domain and path attribute\nwill indicate where it comes from. If it is associated with an URI and\nits domain and path attribute are not specified, given URI will indicate\nwhere this cookie comes from.\n\nIf a cookie corresponding to the given URI already exists,\nthen it is replaced with the new one.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|----------|--------------------------------------------------------------------------------------------------------------|\n| `uri` | `URI`: the uri this cookie associated with. if `null`, this cookie will not be associated with an URI \u003cbr /\u003e |\n| `cookie` | `HttpCookie`: the cookie to store \u003cbr /\u003e |\n\n| Throws ||\n|-------------------------------------------------------------------|-----------------------|\n| [NullPointerException](/reference/java/lang/NullPointerException) | if `cookie` is `null` |\n\n**See also:**\n\n- [get(URI)](/reference/java/net/CookieStore#get(java.net.URI)) \n\n### get\n\nAdded in [API level 9](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract List\u003cHttpCookie\u003e get (URI uri)\n```\n\nRetrieve cookies associated with given URI, or whose domain matches the\ngiven URI. Only cookies that have not expired are returned.\nThis is called for every outgoing HTTP request.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|-------|------------------------------------------------------------------|\n| `uri` | `URI`: the uri associated with the cookies to be returned \u003cbr /\u003e |\n\n| Returns ||\n|-------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------|\n| [List](/reference/java/util/List)`\u003c`[HttpCookie](/reference/java/net/HttpCookie)`\u003e` | an immutable list of HttpCookie, return empty list if no cookies match the given URI \u003cbr /\u003e |\n\n| Throws ||\n|-------------------------------------------------------------------|--------------------|\n| [NullPointerException](/reference/java/lang/NullPointerException) | if `uri` is `null` |\n\n**See also:**\n\n- [add(URI, HttpCookie)](/reference/java/net/CookieStore#add(java.net.URI,%20java.net.HttpCookie)) \n\n### getCookies\n\nAdded in [API level 9](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract List\u003cHttpCookie\u003e getCookies ()\n```\n\nGet all not-expired cookies in cookie store.\n\n\u003cbr /\u003e\n\n| Returns ||\n|-------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------|\n| [List](/reference/java/util/List)`\u003c`[HttpCookie](/reference/java/net/HttpCookie)`\u003e` | an immutable list of http cookies; return empty list if there's no http cookie in store \u003cbr /\u003e |\n\n### getURIs\n\nAdded in [API level 9](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract List\u003cURI\u003e getURIs ()\n```\n\nGet all URIs which identify the cookies in this cookie store.\n\n\u003cbr /\u003e\n\n| Returns ||\n|-----------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------|\n| [List](/reference/java/util/List)`\u003c`[URI](/reference/java/net/URI)`\u003e` | an immutable list of URIs; return empty list if no cookie in this cookie store is associated with an URI \u003cbr /\u003e |\n\n### remove\n\nAdded in [API level 9](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract boolean remove (URI uri, \n HttpCookie cookie)\n```\n\nRemove a cookie from store.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `uri` | `URI`: the uri this cookie associated with. if `null`, the cookie to be removed is not associated with an URI when added; if not `null`, the cookie to be removed is associated with the given URI when added. \u003cbr /\u003e |\n| `cookie` | `HttpCookie`: the cookie to remove \u003cbr /\u003e |\n\n| Returns ||\n|-----------|------------------------------------------------------------|\n| `boolean` | `true` if this store contained the specified cookie \u003cbr /\u003e |\n\n| Throws ||\n|-------------------------------------------------------------------|-----------------------|\n| [NullPointerException](/reference/java/lang/NullPointerException) | if `cookie` is `null` |\n\n### removeAll\n\nAdded in [API level 9](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract boolean removeAll ()\n```\n\nRemove all cookies in this cookie store.\n\n\u003cbr /\u003e\n\n| Returns ||\n|-----------|-------------------------------------------------------------|\n| `boolean` | `true` if this store changed as a result of the call \u003cbr /\u003e |"]]