Package java.net
Class HttpCookie
java.lang.Object
java.net.HttpCookie
- All Implemented Interfaces:
Cloneable
public final class HttpCookie extends Object implements Cloneable
An opaque key-value value pair held by an HTTP client to permit a stateful
session with an HTTP server. This class parses cookie headers for all three
commonly used HTTP cookie specifications:
- The Netscape cookie spec is officially obsolete but widely used in
practice. Each cookie contains one key-value pair and the following
attributes:
Domain,Expires,Path, andSecure. Theversionof cookies in this format is0.There are no accessors for the
Expiresattribute. When parsed, expires attributes are assigned to theMax-Ageattribute as an offset fromnow. - RFC 2109 formalizes
the Netscape cookie spec. It replaces the
Expirestimestamp with aMax-Ageduration and addsCommentandVersionattributes. Theversionof cookies in this format is1. - RFC 2965 refines
RFC 2109. It adds
Discard,Port, andCommentURLattributes and renames the header fromSet-CookietoSet-Cookie2. Theversionof cookies in this format is1.
This implementation silently discards unrecognized attributes. In
particular, the HttpOnly attribute is widely served but isn't in any
of the above specs. It was introduced by Internet Explorer to prevent server
cookies from being exposed in the DOM to JavaScript, etc.
- Since:
- 1.6
-
Constructor Summary
Constructors Constructor Description HttpCookie(String name, String value)Creates a new cookie. -
Method Summary
Modifier and Type Method Description Objectclone()Creates and returns a copy of thisObject.static booleandomainMatches(String domainPattern, String host)Returns true ifhostmatches the domain patterndomain.booleanequals(Object object)Returns true ifobjectis a cookie with the same domain, name and path.StringgetComment()Returns theCommentattribute.StringgetCommentURL()Returns the value ofCommentURLattribute.booleangetDiscard()Returns theDiscardattribute.StringgetDomain()Returns theDomainattribute.longgetMaxAge()Returns theMax-Ageattribute, in delta-seconds.StringgetName()Returns the name of this cookie.StringgetPath()Returns thePathattribute.StringgetPortlist()Returns thePortattribute, usually containing comma-separated port numbers.booleangetSecure()Returns theSecureattribute.StringgetValue()Returns the value of this cookie.intgetVersion()Returns the version of this cookie.booleanhasExpired()Returns true if this cookie's Max-Age is 0.inthashCode()Returns the hash code of this HTTP cookie:static List<HttpCookie>parse(String header)Constructs a cookie from a string.voidsetComment(String comment)Set theCommentattribute of this cookie.voidsetCommentURL(String commentURL)Set theCommentURLattribute of this cookie.voidsetDiscard(boolean discard)Set theDiscardattribute of this cookie.voidsetDomain(String pattern)Set theDomainattribute of this cookie.voidsetMaxAge(long deltaSeconds)Sets theMax-Ageattribute of this cookie.voidsetPath(String path)Set thePathattribute of this cookie.voidsetPortlist(String portList)Set thePortattribute of this cookie.voidsetSecure(boolean secure)Sets theSecureattribute of this cookie.voidsetValue(String value)Sets the opaque value of this cookie.voidsetVersion(int newVersion)Sets theVersionattribute of the cookie.StringtoString()Returns a string representing this cookie in the format used by theCookieheader line in an HTTP request.
-
Constructor Details
-
HttpCookie
Creates a new cookie.- Parameters:
name- a non-empty string that contains only printable ASCII, no commas or semicolons, and is not prefixed with$. May not be an HTTP attribute name.value- an opaque value from the HTTP server.- Throws:
IllegalArgumentException- ifnameis invalid.
-
-
Method Details
-
domainMatches
Returns true ifhostmatches the domain patterndomain.- Parameters:
domainPattern- a host name (likeandroid.comorlocalhost), or a pattern to match subdomains of a domain name (like.android.com). A special case pattern is.local, which matches all hosts without a TLD (likelocalhost).host- the host name or IP address from an HTTP request.
-
parse
Constructs a cookie from a string. The string should comply with set-cookie or set-cookie2 header format as specified in RFC 2965. Since set-cookies2 syntax allows more than one cookie definitions in one header, the returned object is a list.- Parameters:
header- a set-cookie or set-cookie2 header.- Returns:
- a list of constructed cookies
- Throws:
IllegalArgumentException- if the string does not comply with cookie specification, or the cookie name contains illegal characters, or reserved tokens of cookie specification appearsNullPointerException- if header is null
-
getComment
Returns theCommentattribute. -
getCommentURL
Returns the value ofCommentURLattribute. -
getDiscard
public boolean getDiscard()Returns theDiscardattribute. -
getDomain
Returns theDomainattribute. -
getMaxAge
public long getMaxAge()Returns theMax-Ageattribute, in delta-seconds. -
getName
Returns the name of this cookie. -
getPath
Returns thePathattribute. This cookie is visible to all subpaths. -
getPortlist
Returns thePortattribute, usually containing comma-separated port numbers. A null port indicates that the cookie may be sent to any port. The empty string indicates that the cookie should only be sent to the port of the originating request. -
getSecure
public boolean getSecure()Returns theSecureattribute. -
getValue
Returns the value of this cookie. -
getVersion
public int getVersion()Returns the version of this cookie. -
hasExpired
public boolean hasExpired()Returns true if this cookie's Max-Age is 0. -
setComment
Set theCommentattribute of this cookie. -
setCommentURL
Set theCommentURLattribute of this cookie. -
setDiscard
public void setDiscard(boolean discard)Set theDiscardattribute of this cookie. -
setDomain
Set theDomainattribute of this cookie. HTTP clients send cookies only to matching domains. -
setMaxAge
public void setMaxAge(long deltaSeconds)Sets theMax-Ageattribute of this cookie. -
setPath
Set thePathattribute of this cookie. HTTP clients send cookies to this path and its subpaths. -
setPortlist
Set thePortattribute of this cookie. -
setSecure
public void setSecure(boolean secure)Sets theSecureattribute of this cookie. -
setValue
Sets the opaque value of this cookie. -
setVersion
public void setVersion(int newVersion)Sets theVersionattribute of the cookie.- Throws:
IllegalArgumentException- if v is neither 0 nor 1
-
clone
Description copied from class:ObjectCreates and returns a copy of thisObject. The default implementation returns a so-called "shallow" copy: It creates a new instance of the same class and then copies the field values (including object references) from this instance to the new instance. A "deep" copy, in contrast, would also recursively clone nested objects. A subclass that needs to implement this kind of cloning should callsuper.clone()to create the new instance and then create deep copies of the nested, mutable objects. -
equals
Returns true ifobjectis a cookie with the same domain, name and path. Domain and name use case-insensitive comparison; path uses a case-sensitive comparison.- Overrides:
equalsin classObject- Parameters:
object- the object to compare this instance with.- Returns:
trueif the specified object is equal to thisObject;falseotherwise.- See Also:
Object.hashCode()
-
hashCode
public int hashCode()Returns the hash code of this HTTP cookie:name.toLowerCase(Locale.US).hashCode() + (domain == null ? 0 : domain.toLowerCase(Locale.US).hashCode()) + (path == null ? 0 : path.hashCode())- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
toString
Returns a string representing this cookie in the format used by theCookieheader line in an HTTP request.
-