public class

HttpCookie

extends Object
java.lang.Object
   ↳ com.neomades.io.http.HttpCookie

Class Overview

An HttpCookie object represents an HTTP cookie, which carries state information between server and user agent. This class supports several cookie versions but prefer to use the original Netscape cookie format which is well supported by all the platforms (getVersion() = 0).
Other versions are not supported by all the platforms. iOS supports RFC 6265 while Android supports old deprecated RFC 2109 and 2965. iOS may understand header relying on RFC 2109 but the result is not granted.

Summary

Public Constructors
HttpCookie(String name, String value)
Build a default cookie with a name and a value.
Public Methods
String getComment()
Returns the comment describing the purpose of this cookie, or null if the cookie has no comment.
String getCommentURL()
Returns the comment URL describing the purpose of this cookie, or null if the cookie has no comment URL.
boolean getDiscard()
Returns the discard attribute of the cookie.
String getDomain()
Returns the domain name set for this cookie.
long getMaxAge()
Returns the maximum age of the cookie, specified in seconds.
String getName()
Returns the name of the cookie.
String getPath()
Returns the path on the server to which the browser returns this cookie.
String getPortList()
Returns the port list attribute of the cookie or an empty string by default.
boolean getSecure()
Returns true if sending this cookie should be restricted to a secure protocol, or false if the it can be sent using any protocol.
String getValue()
Returns the value of the cookie.
int getVersion()
Returns the version of the protocol this cookie complies with.
boolean hasExpired()
Reports whether this HTTP cookie has expired or not.
boolean isHttpOnly()
Returns true if this cookie contains the "HttpOnly" attribute.
static List<HttpCookie> parse(String header)
Try to parse a cookie header string and to convert it into a list of HttpCookie.
String toString()
Returns the String corresponding to the cookie (header format).
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public HttpCookie (String name, String value)

Build a default cookie with a name and a value.
the name can contain only ASCII alphanumeric characters and cannot contain commas, semicolons, or white space or begin with a $ character.

Parameters
name name of the cookie
value value of the cookie
Throws
NullPointerException if the name or the value is null

Public Methods

public String getComment ()

Returns the comment describing the purpose of this cookie, or null if the cookie has no comment.
This is an old RFC 2965 value. This should not be used.

Returns
  • the comment describing the purpose of this cookie

public String getCommentURL ()

Returns the comment URL describing the purpose of this cookie, or null if the cookie has no comment URL.
This is an old RFC 2965 value. This should not be used.

Returns
  • the comment URL describing the purpose of this cookie

public boolean getDiscard ()

Returns the discard attribute of the cookie.
For version 0 (netscape), the returned value is always false. For other versions, the returned value depends on the expiration date of the cookie.
This is an old RFC 2965 value. This should not be used.

Cross platform considerations

When parsing a header, in Android, the discard value relies only on the "Discard" attribute of the cookie header and is always false if the header value is not set. In iOS it is true for cookie version > 0 if there is no expiration date, even if "Discard" was not set in the header.

Returns
  • this cookie's discard attribute

public String getDomain ()

Returns the domain name set for this cookie. For compatibility with old browser, a leading dot may be added to the domain value.

Returns
  • the domain name set for this cookie

public long getMaxAge ()

Returns the maximum age of the cookie, specified in seconds. By default, -1 or 0 indicating the cookie will persist until browser shutdown.

Cross platform considerations

In iOS, when parsing a header, this value may not be filled because iOS needs to know the server date to correctly fill this value.

Returns
  • the maximum age of the cookie

public String getName ()

Returns the name of the cookie.

Returns
  • the name of the cookie

public String getPath ()

Returns the path on the server to which the browser returns this cookie. The cookie is visible to all subpaths on the server. By default the path is "/" which means means the cookie will be sent for all URLs in the domain.

Returns
  • the path on the server to which the browser returns this cookie

public String getPortList ()

Returns the port list attribute of the cookie or an empty string by default.
This is an old RFC 2965 value. This should not be used.

Returns
  • the port list attribute of the cookie or an empty string

public boolean getSecure ()

Returns true if sending this cookie should be restricted to a secure protocol, or false if the it can be sent using any protocol.

Returns
  • false if the cookie can be sent over any standard protocol; otherwise, true

public String getValue ()

Returns the value of the cookie.

Returns
  • the value of the cookie

public int getVersion ()

Returns the version of the protocol this cookie complies with. Version 0 is for the original Netscape cookie format which is well supported by all the platforms. Other versions depends on the platform. They rely on RFC 2965, 2109 or 6265.

Returns
  • 0 if the cookie complies with the original Netscape specification, an integer > 0 otherwise

public boolean hasExpired ()

Reports whether this HTTP cookie has expired or not.

Returns
  • true to indicate this HTTP cookie has expired; otherwise, false

public boolean isHttpOnly ()

Returns true if this cookie contains the "HttpOnly" attribute. This means that the cookie should not be accessible to scripting engines, like javascript.

Returns
  • true if this cookie should be considered HTTPOnly

public static List<HttpCookie> parse (String header)

Try to parse a cookie header string and to convert it into a list of HttpCookie. It may return several cookies since some cookie RFC enable to have several cookies in the same header key (Set-Cookie2 or Cookie for example).
The header can be passed with or without its key.
For example calling:

 HttpCookie.parse("Set-Cookie: name=value");
 
is the same as calling
 HttpCookie.parse("name=value");
 

If the header is not a cookie header, this method will throw an IllegalArgumentException.

Parameters
header the header to parse
Returns
  • a List of cookie parsed from header line string
Throws
IllegalArgumentException if the header cannot be parsed

public String toString ()

Returns the String corresponding to the cookie (header format). Some properties will not be present in the returned String: Expires, Max-Age, Discard and Version. If the returned String must be send to a server, do not forget to add this information if required. The header key (Set-Cookie, Cookie, Set-Cookie2) is not returned.

Returns
  • a string representation of the cookie