Skip to content

Most visited

Recently visited

navigation
Added in API level 9

HttpCookie

public final class HttpCookie
extends Object implements Cloneable

java.lang.Object
   ↳ java.net.HttpCookie


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:

Support for the "HttpOnly" attribute specified in RFC 6265 is also included. RFC 6265 is intended to obsolete RFC 2965. Support for features from RFC 2965 that have been deprecated by RFC 6265 such as Cookie2, Set-Cookie2 headers and version information remain supported by this class.

This implementation silently discards unrecognized attributes.

Summary

Public constructors

HttpCookie(String name, String value)

Creates a new cookie.

Public methods

Object clone()

Creates and returns a copy of this Object.

static boolean domainMatches(String domainPattern, String host)

Returns true if host matches the domain pattern domain.

boolean equals(Object object)

Returns true if object is a cookie with the same domain, name and path.

String getComment()

Returns the Comment attribute.

String getCommentURL()

Returns the value of CommentURL attribute.

boolean getDiscard()

Returns the Discard attribute.

String getDomain()

Returns the Domain attribute.

long getMaxAge()

Returns the Max-Age attribute, in delta-seconds.

String getName()

Returns the name of this cookie.

String getPath()

Returns the Path attribute.

String getPortlist()

Returns the Port attribute, usually containing comma-separated port numbers.

boolean getSecure()

Returns the Secure attribute.

String getValue()

Returns the value of this cookie.

int getVersion()

Returns the version of this cookie.

boolean hasExpired()

Returns true if this cookie's Max-Age is 0.

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())
 

static List<HttpCookie> parse(String header)

Constructs a cookie from a string.

void setComment(String comment)

Set the Comment attribute of this cookie.

void setCommentURL(String commentURL)

Set the CommentURL attribute of this cookie.

void setDiscard(boolean discard)

Set the Discard attribute of this cookie.

void setDomain(String pattern)

Set the Domain attribute of this cookie.

void setMaxAge(long deltaSeconds)

Sets the Max-Age attribute of this cookie.

void setPath(String path)

Set the Path attribute of this cookie.

void setPortlist(String portList)

Set the Port attribute of this cookie.

void setSecure(boolean secure)

Sets the Secure attribute of this cookie.

void setValue(String value)

Sets the opaque value of this cookie.

void setVersion(int newVersion)

Sets the Version attribute of the cookie.

String toString()

Returns a string representing this cookie in the format used by the Cookie header line in an HTTP request as specified by RFC 2965 section 3.3.4.

Inherited methods

From class java.lang.Object

Public constructors

HttpCookie

Added in API level 9
HttpCookie (String name, 
                String value)

Creates a new cookie.

Parameters
name String: 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 String: an opaque value from the HTTP server.
Throws
IllegalArgumentException if name is invalid.

Public methods

clone

Added in API level 9
Object clone ()

Creates and returns a copy of this Object. 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 call super.clone() to create the new instance and then create deep copies of the nested, mutable objects.

Returns
Object a copy of this object.

domainMatches

Added in API level 9
boolean domainMatches (String domainPattern, 
                String host)

Returns true if host matches the domain pattern domain.

Parameters
domainPattern String: a host name (like android.com or localhost), 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 (like localhost).
host String: the host name or IP address from an HTTP request.
Returns
boolean

equals

Added in API level 9
boolean equals (Object object)

Returns true if object is a cookie with the same domain, name and path. Domain and name use case-insensitive comparison; path uses a case-sensitive comparison.

Parameters
object Object: the object to compare this instance with.
Returns
boolean true if the specified object is equal to this Object; false otherwise.

getComment

Added in API level 9
String getComment ()

Returns the Comment attribute.

Returns
String

getCommentURL

Added in API level 9
String getCommentURL ()

Returns the value of CommentURL attribute.

Returns
String

getDiscard

Added in API level 9
boolean getDiscard ()

Returns the Discard attribute.

Returns
boolean

getDomain

Added in API level 9
String getDomain ()

Returns the Domain attribute.

Returns
String

getMaxAge

Added in API level 9
long getMaxAge ()

Returns the Max-Age attribute, in delta-seconds.

Returns
long

getName

Added in API level 9
String getName ()

Returns the name of this cookie.

Returns
String

getPath

Added in API level 9
String getPath ()

Returns the Path attribute. This cookie is visible to all subpaths.

Returns
String

getPortlist

Added in API level 9
String getPortlist ()

Returns the Port attribute, 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.

Returns
String

getSecure

Added in API level 9
boolean getSecure ()

Returns the Secure attribute.

Returns
boolean

getValue

Added in API level 9
String getValue ()

Returns the value of this cookie.

Returns
String

getVersion

Added in API level 9
int getVersion ()

Returns the version of this cookie.

Returns
int

hasExpired

Added in API level 9
boolean hasExpired ()

Returns true if this cookie's Max-Age is 0.

Returns
boolean

hashCode

Added in API level 9
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())
 

Returns
int this object's hash code.

parse

Added in API level 9
List<HttpCookie> parse (String header)

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 String: a set-cookie or set-cookie2 header.
Returns
List<HttpCookie> 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 appears
NullPointerException if header is null

setComment

Added in API level 9
void setComment (String comment)

Set the Comment attribute of this cookie.

Parameters
comment String

setCommentURL

Added in API level 9
void setCommentURL (String commentURL)

Set the CommentURL attribute of this cookie.

Parameters
commentURL String

setDiscard

Added in API level 9
void setDiscard (boolean discard)

Set the Discard attribute of this cookie.

Parameters
discard boolean

setDomain

Added in API level 9
void setDomain (String pattern)

Set the Domain attribute of this cookie. HTTP clients send cookies only to matching domains.

Parameters
pattern String

setMaxAge

Added in API level 9
void setMaxAge (long deltaSeconds)

Sets the Max-Age attribute of this cookie.

Parameters
deltaSeconds long

setPath

Added in API level 9
void setPath (String path)

Set the Path attribute of this cookie. HTTP clients send cookies to this path and its subpaths.

Parameters
path String

setPortlist

Added in API level 9
void setPortlist (String portList)

Set the Port attribute of this cookie.

Parameters
portList String

setSecure

Added in API level 9
void setSecure (boolean secure)

Sets the Secure attribute of this cookie.

Parameters
secure boolean

setValue

Added in API level 9
void setValue (String value)

Sets the opaque value of this cookie.

Parameters
value String

setVersion

Added in API level 9
void setVersion (int newVersion)

Sets the Version attribute of the cookie.

Parameters
newVersion int
Throws
IllegalArgumentException if v is neither 0 nor 1

toString

Added in API level 9
String toString ()

Returns a string representing this cookie in the format used by the Cookie header line in an HTTP request as specified by RFC 2965 section 3.3.4.

The resulting string does not include a "Cookie:" prefix or any version information. The returned String is not suitable for passing to parse(String): Several of the attributes that would be needed to preserve all of the cookie's information are omitted. The String is formatted for an HTTP request not an HTTP response.

The attributes included and the format depends on the cookie's version:

  • Version 0: Includes only the name and value. Conforms to RFC 2965 (for version 0 cookies). This should also be used to conform with RFC 6265.
  • Version 1: Includes the name and value, and Path, Domain and Port attributes. Conforms to RFC 2965 (for version 1 cookies).

Returns
String a printable representation of this object.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.