Module org.htmlunit
Package org.htmlunit

Class CookieManager

java.lang.Object
org.htmlunit.CookieManager
All Implemented Interfaces:
Serializable

public class CookieManager extends Object implements 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 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 - true to enable cookie support, false to disable it
    • isCookiesEnabled

      public boolean isCookiesEnabled()
      Returns true if cookies are enabled. Cookies are enabled by default.
      Returns:
      true if cookies are enabled, false otherwise
    • getCookies

      public Set<Cookie> 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

      public boolean clearExpired(Date date)
      Removes all cookies whose expiration date is strictly before the given date. A cookie expiring at exactly date is not considered expired by this comparison (consistent with RFC 6265's treatment of cookie expiry).
      Parameters:
      date - the date to compare against; if null, this method does nothing and returns false
      Returns:
      true if one or more cookies were found expired and removed; false otherwise, or if cookie support is disabled
    • getCookie

      public Cookie getCookie(String name)
      Returns the currently configured cookie with the specified name, or null if 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 be null
      Returns:
      the matching cookie, or null if none exists or cookie support is disabled
    • addCookie

      public void addCookie(Cookie cookie)
      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

      public void removeCookie(Cookie cookie)
      Removes the specified cookie, if present. Does nothing if cookie support is disabled.
      Parameters:
      cookie - the cookie to remove; may be null, in which case this method does nothing
    • clearCookies

      public void clearCookies()
      Removes all cookies from this manager. Does nothing if cookie support is disabled.