Package org.htmlunit
Class CookieManager
- java.lang.Object
-
- org.htmlunit.CookieManager
-
- All Implemented Interfaces:
java.io.Serializable
public class CookieManager extends java.lang.Object implements java.io.SerializableManages cookies for aWebClient. 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 voidaddCookie(Cookie cookie)Adds the specified cookie.org.apache.http.cookie.CookieOriginbuildCookieOrigin(java.net.URL url)Helper that builds a CookieOrigin.voidclearCookies()Removes all cookies.booleanclearExpired(java.util.Date date)Clears all cookies that have expired before supplied date.CookiegetCookie(java.lang.String name)Returns the currently configured cookie with the specified name, ornullif one does not exist.java.util.Set<Cookie>getCookies()Returns the currently configured cookies, in an unmodifiable set.protected intgetPort(java.net.URL url)Gets the port of the URL.booleanisCookiesEnabled()Returnstrueif cookies are enabled.voidremoveCookie(Cookie cookie)Removes the specified cookie.java.net.URLreplaceForCookieIfNecessary(java.net.URL url)CookieOrigindoesn't like empty hosts and negative ports, but these things happen if we're dealing with a local file.voidsetCookiesEnabled(boolean enabled)Enables/disables cookie support.
-
-
-
Method Detail
-
setCookiesEnabled
public void setCookiesEnabled(boolean enabled)
Enables/disables cookie support. Cookies are enabled by default.- Parameters:
enabled-trueto enable cookie support,falseotherwise
-
isCookiesEnabled
public boolean isCookiesEnabled()
Returnstrueif cookies are enabled. Cookies are enabled by default.- Returns:
trueif cookies are enabled,falseotherwise
-
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)
CookieOrigindoesn'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, ornullif 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
nullif 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.
-
-