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.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addCookie(org.htmlunit.http.Cookie cookie)
    Adds the specified cookie, replacing any existing cookie considered equal to it.
    void
    Removes all cookies from this manager.
    boolean
    Removes all cookies whose expiration date is strictly before the given date.
    org.htmlunit.http.Cookie
    Returns the currently configured cookie with the specified name, or null if none exists.
    Set<org.htmlunit.http.Cookie>
    Returns a snapshot of the currently configured cookies as an unmodifiable set.
    boolean
    Returns true if cookies are enabled.
    void
    removeCookie(org.htmlunit.http.Cookie cookie)
    Removes the specified cookie, if present.
    void
    setCookiesEnabled(boolean enabled)
    Enables or disables cookie support.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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<org.htmlunit.http.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 org.htmlunit.http.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(org.htmlunit.http.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(org.htmlunit.http.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.