Skip to content

Most visited

Recently visited

navigation
Added in API level 9

CookieManager

public class CookieManager
extends CookieHandler

java.lang.Object
   ↳ java.net.CookieHandler
     ↳ java.net.CookieManager


This class provides a concrete implementation of CookieHandler. It separates the storage of cookies from the policy which decides to accept or deny cookies. The constructor can have two arguments: a CookieStore and a CookiePolicy. The former is in charge of cookie storage and the latter makes decision on acceptance/rejection. CookieHandler is in the center of cookie management. User can make use of CookieHandler.setDefault to set a CookieManager as the default one used. CookieManager.put uses CookiePolicy.shouldAccept to decide whether to put some cookies into a cookie store. Three built-in CookiePolicy is defined: ACCEPT_ALL, ACCEPT_NONE and ACCEPT_ORIGINAL_SERVER. Users can also customize the policy by implementing CookiePolicy. Any accepted HTTP cookie is stored in CookieStore and users can also have their own implementation. Up to now, Only add(URI, HttpCookie) and get(URI) are used by CookieManager. Other methods in this class may probably be used in a more complicated implementation. There are many ways to customize user's own HTTP cookie management: First, call CookieHandler.setDefault to set a new CookieHandler implementation. Second, call CookieHandler.getDefault to use CookieManager. The CookiePolicy and CookieStore used are customized. Third, use the customized CookiePolicy and the CookieStore. This implementation conforms to RFC 2965 section 3.3.

Summary

Public constructors

CookieManager()

Constructs a new cookie manager.

CookieManager(CookieStore store, CookiePolicy cookiePolicy)

Constructs a new cookie manager using a specified cookie store and a cookie policy.

Public methods

Map<StringList<String>> get(URI uri, Map<StringList<String>> requestHeaders)

Searches and gets all cookies in the cache by the specified uri in the request header.

CookieStore getCookieStore()

Gets current cookie store.

void put(URI uri, Map<StringList<String>> responseHeaders)

Sets cookies according to uri and responseHeaders

void setCookiePolicy(CookiePolicy cookiePolicy)

Sets the cookie policy of this cookie manager.

Inherited methods

From class java.net.CookieHandler
From class java.lang.Object

Public constructors

CookieManager

Added in API level 9
CookieManager ()

Constructs a new cookie manager. The invocation of this constructor is the same as the invocation of CookieManager(null, null).

CookieManager

Added in API level 9
CookieManager (CookieStore store, 
                CookiePolicy cookiePolicy)

Constructs a new cookie manager using a specified cookie store and a cookie policy.

Parameters
store CookieStore: a CookieStore to be used by cookie manager. The manager will use a default one if the arg is null.
cookiePolicy CookiePolicy: a CookiePolicy to be used by cookie manager ACCEPT_ORIGINAL_SERVER will be used if the arg is null.

Public methods

get

Added in API level 9
Map<StringList<String>> get (URI uri, 
                Map<StringList<String>> requestHeaders)

Searches and gets all cookies in the cache by the specified uri in the request header.

Parameters
uri URI: the specified uri to search for
requestHeaders Map: a list of request headers
Returns
Map<StringList<String>> a map that record all such cookies, the map is unchangeable
Throws
IOException if some error of I/O operation occurs

getCookieStore

Added in API level 9
CookieStore getCookieStore ()

Gets current cookie store.

Returns
CookieStore the cookie store currently used by cookie manager.

put

Added in API level 9
void put (URI uri, 
                Map<StringList<String>> responseHeaders)

Sets cookies according to uri and responseHeaders

Parameters
uri URI: the specified uri
responseHeaders Map: a list of request headers
Throws
IOException if some error of I/O operation occurs

setCookiePolicy

Added in API level 9
void setCookiePolicy (CookiePolicy cookiePolicy)

Sets the cookie policy of this cookie manager. ACCEPT_ORIGINAL_SERVER is the default policy for CookieManager.

Parameters
cookiePolicy CookiePolicy: the cookie policy. if null, the original policy will not be changed.
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.