java.lang.Object
org.htmlunit.CookieManager
- All Implemented Interfaces:
Serializable
Manages cookies for a
WebClient.
This class is thread-safe: all mutator and accessor methods are
synchronized on the CookieManager instance.
Cookie support can be turned off via setCookiesEnabled(boolean).
While disabled, this manager ignores all cookie operations: additions and
removals become no-ops, and accessors behave as if the cookie store were
empty. Subclasses that override these methods must preserve this contract.
- Author:
- Daniel Gredler, Ahmed Ashour, Nicolas Belisle, Ronald Brill
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds the specified cookie, replacing any existing cookie considered equal to it.voidRemoves all cookies from this manager.booleanclearExpired(Date date) Removes all cookies whose expiration date is strictly before the given date.Returns the currently configured cookie with the specified name, ornullif none exists.Returns a snapshot of the currently configured cookies as an unmodifiable set.booleanReturnstrueif cookies are enabled.voidremoveCookie(Cookie cookie) Removes the specified cookie, if present.voidsetCookiesEnabled(boolean enabled) Enables or disables cookie support.
-
Constructor Details
-
CookieManager
public CookieManager()Creates a new instance.
-
-
Method Details
-
setCookiesEnabled
public void setCookiesEnabled(boolean enabled) Enables or disables cookie support. Cookies are enabled by default. Disabling does not clear existing cookies; it only suppresses all cookie operations until re-enabled.- Parameters:
enabled-trueto enable cookie support,falseto disable it
-
isCookiesEnabled
public boolean isCookiesEnabled()Returnstrueif cookies are enabled. Cookies are enabled by default.- Returns:
trueif cookies are enabled,falseotherwise
-
getCookies
Returns a snapshot of the currently configured cookies as an unmodifiable set. The returned set is a copy and will not reflect later changes to this manager's cookie store.- Returns:
- the currently configured cookies, in an unmodifiable set; empty if cookie support is disabled
-
clearExpired
Removes all cookies whose expiration date is strictly before the given date. A cookie expiring at exactlydateis not considered expired by this comparison (consistent with RFC 6265's treatment of cookie expiry).- Parameters:
date- the date to compare against; ifnull, this method does nothing and returnsfalse- Returns:
trueif one or more cookies were found expired and removed;falseotherwise, or if cookie support is disabled
-
getCookie
Returns the currently configured cookie with the specified name, ornullif none exists. If multiple stored cookies happen to share a name (e.g., differing by domain or path), the first match encountered in iteration order is returned.- Parameters:
name- the cookie name to look up; may benull- Returns:
- the matching cookie, or
nullif none exists or cookie support is disabled
-
addCookie
Adds the specified cookie, replacing any existing cookie considered equal to it. If the cookie is already expired relative to the current time, it replaces the old entry (if any) but is not itself re-added — this is the mechanism by which a cookie can be deleted by supplying a past expiration date, per RFC 6265.- Parameters:
cookie- the cookie to add
-
removeCookie
Removes the specified cookie, if present. Does nothing if cookie support is disabled.- Parameters:
cookie- the cookie to remove; may benull, in which case this method does nothing
-
clearCookies
public void clearCookies()Removes all cookies from this manager. Does nothing if cookie support is disabled.
-