Package org.htmlunit

Class CookieManager

  • All Implemented Interfaces:
    java.io.Serializable

    public class CookieManager
    extends java.lang.Object
    implements java.io.Serializable
    Manages cookies for a WebClient. This class is thread-safe. You can disable Cookies by calling setCookiesEnabled(false). The CookieManager itself takes care of this and ignores all cookie request if disabled. If you override this your methods have to do the same.
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      CookieManager()
      Creates a new instance.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addCookie​(Cookie cookie)
      Adds the specified cookie.
      org.apache.http.cookie.CookieOrigin buildCookieOrigin​(java.net.URL url)
      Helper that builds a CookieOrigin.
      void clearCookies()
      Removes all cookies.
      boolean clearExpired​(java.util.Date date)
      Clears all cookies that have expired before supplied date.
      Cookie getCookie​(java.lang.String name)
      Returns the currently configured cookie with the specified name, or null if one does not exist.
      java.util.Set<Cookie> getCookies()
      Returns the currently configured cookies, in an unmodifiable set.
      protected int getPort​(java.net.URL url)
      Gets the port of the URL.
      boolean isCookiesEnabled()
      Returns true if cookies are enabled.
      void removeCookie​(Cookie cookie)
      Removes the specified cookie.
      java.net.URL replaceForCookieIfNecessary​(java.net.URL url)
      CookieOrigin doesn't like empty hosts and negative ports, but these things happen if we're dealing with a local file.
      void setCookiesEnabled​(boolean enabled)
      Enables/disables cookie support.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CookieManager

        public CookieManager()
        Creates a new instance.
    • Method Detail

      • setCookiesEnabled

        public void setCookiesEnabled​(boolean enabled)
        Enables/disables cookie support. Cookies are enabled by default.
        Parameters:
        enabled - true to enable cookie support, false otherwise
      • 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 java.util.Set<Cookie> getCookies()
        Returns the currently configured cookies, in an unmodifiable set. If disabled, this returns an empty set.
        Returns:
        the currently configured cookies, in an unmodifiable set
      • buildCookieOrigin

        public org.apache.http.cookie.CookieOrigin buildCookieOrigin​(java.net.URL url)
        Helper that builds a CookieOrigin.
        Parameters:
        url - the url to be used
        Returns:
        the new CookieOrigin
      • clearExpired

        public boolean clearExpired​(java.util.Date date)
        Clears all cookies that have expired before supplied date. If disabled, this returns false.
        Parameters:
        date - the date to use for comparison when clearing expired cookies
        Returns:
        whether any cookies were found expired, and were cleared
      • getPort

        protected int getPort​(java.net.URL url)
        Gets the port of the URL. This functionality is implemented here as protected method to allow subclass to change it as workaround to Google App Engine bug 4784.
        Parameters:
        url - the URL
        Returns:
        the port use to connect the server
      • replaceForCookieIfNecessary

        public java.net.URL replaceForCookieIfNecessary​(java.net.URL url)
        CookieOrigin doesn't like empty hosts and negative ports, but these things happen if we're dealing with a local file. This method allows us to work around this limitation in HttpClient by feeding it a bogus host and port.
        Parameters:
        url - the URL to replace if necessary
        Returns:
        the replacement URL, or the original URL if no replacement was necessary
      • getCookie

        public Cookie getCookie​(java.lang.String name)
        Returns the currently configured cookie with the specified name, or null if one does not exist. If disabled, this returns null.
        Parameters:
        name - the name of the cookie to return
        Returns:
        the currently configured cookie with the specified name, or null if one does not exist
      • addCookie

        public void addCookie​(Cookie cookie)
        Adds the specified cookie. If disabled, this does nothing.
        Parameters:
        cookie - the cookie to add
      • removeCookie

        public void removeCookie​(Cookie cookie)
        Removes the specified cookie. If disabled, this does nothing.
        Parameters:
        cookie - the cookie to remove
      • clearCookies

        public void clearCookies()
        Removes all cookies. If disabled, this does nothing.