Package com.meterware.httpunit.cookies

# Class CookieJar

java.lang.Object
com.meterware.httpunit.cookies.CookieJar

public class CookieJar extends Object
A collection of HTTP cookies, which can interact with cookie and set-cookie header values.

Author:
  Russell Gold, Drew Varner

## Constructor Summary

Constructors
Constructor
Description
`CookieJar()`
Creates an empty cookie jar.
`CookieJar(CookieSource source)`
Creates a cookie jar which is initially populated with cookies parsed from the `Set-Cookie` and `Set-Cookie2` header fields.

## Method Summary

Modifier and Type
Method
Description
`void`
`addCookie(String name, String value)`
Deprecated.
as of 1.6, use #putCookie
`void`
`clear()`
Empties this cookie jar of all contents.
`Cookie`
`getCookie(String name)`
Returns the value of the specified cookie.
`String`
`getCookieHeaderField(URL targetURL)`
Returns the value of the cookie header to be sent to the specified URL.
`String[]`
`getCookieNames()`
Returns the name of all the active cookies in this cookie jar.
`Collection`
`getCookies()`
Returns a collection containing all of the cookies in this jar.
`String`
`getCookieValue(String name)`
Returns the value of the specified cookie.
`void`
`putCookie(String name, String value)`
Defines a cookie to be sent to the server on every request.
`void`
`putSingleUseCookie(String name, String value, String domain, String path)`
Define a non-global cookie.
`void`
`updateCookies(CookieJar newJar)`
Updates the cookies maintained in this cookie jar with those in another cookie jar.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### CookieJar

public CookieJar()
Creates an empty cookie jar.

### CookieJar

public CookieJar(CookieSource source)
Creates a cookie jar which is initially populated with cookies parsed from the `Set-Cookie` and `Set-Cookie2` header fields.

Note that the parsing does not strictly follow the specifications, but attempts to imitate the behavior of popular browsers. Specifically, it allows cookie values to contain commas, which the Netscape standard does not allow for, but which is required by some servers.

## Method Details

### clear

public void clear()
Empties this cookie jar of all contents.

### addCookie

public void addCookie(String name, String value)
Deprecated.
as of 1.6, use #putCookie
Defines a cookie to be sent to the server on every request. This bypasses the normal mechanism by which only certain cookies are sent based on their host and path.

### putCookie

public void putCookie(String name, String value)
Defines a cookie to be sent to the server on every request. This bypasses the normal mechanism by which only certain cookies are sent based on their host and path. Values of null will result in the cookie being removed. Any other value will leave the cookie unchanged expect for the value.

Since:
  1.6

### putSingleUseCookie

public void putSingleUseCookie(String name, String value, String domain, String path)
Define a non-global cookie. This cookie can be overwritten by subsequent cookie definitions in http headers. This cookie definition requires a domain and path. If a global cookie is defined with the same name, this cookie is not added.

### getCookieNames

public String[] getCookieNames()
Returns the name of all the active cookies in this cookie jar.

### getCookies

public Collection getCookies()
Returns a collection containing all of the cookies in this jar.

### getCookieValue

public String getCookieValue(String name)
Returns the value of the specified cookie.

Parameters:
  `name` - - the name of the cookie to get the value for
Returns:
  the value of the cookie

### getCookie

public Cookie getCookie(String name)
Returns the value of the specified cookie.

### getCookieHeaderField

public String getCookieHeaderField(URL targetURL)
Returns the value of the cookie header to be sent to the specified URL. Will return null if no compatible cookie is defined.

### updateCookies

public void updateCookies(CookieJar newJar)
Updates the cookies maintained in this cookie jar with those in another cookie jar. Any duplicate cookies in the new jar will replace those in this jar.