Java Reference
In-Depth Information
erField(int n)
methodswereusedtoaccessaresponse's
Set-Cookie
headers,
andthe
void setRequestProperty(String key, String value)
meth-
od was used to create a request's
Cookie
header.
Note
RFC 2109: HTTP State Management Mechanism (
ht-
Cookie
headers.
Java 5 introduced the abstract
java.net.CookieHandler
class as a callback
mechanismthatconnectsHTTPstatemanagementtoanHTTPprotocolhandler(think
concrete
HttpURLConnection
subclass).Anapplicationinstallsaconcrete
Cook-
ieHandler
subclass as the system-wide cookie handler via the
CookieHandler
class's
void setDefault(CookieHandler cHandler)
classmethod.Acom-
panion
CookieHandler getDefault()
classmethodreturnsthiscookiehandler,
which is null when a system-wide cookie handler hasn't been installed.
An HTTP protocol handler accesses response and request headers. This handler in-
vokes the system-wide cookie handler's
void put(URI uri, Map<String,
List<String>> responseHeaders)
method to store response cookies in a
cookiecache,andinvokesthe
Map<String, List<String>> get(URI uri,
Map<String, List<String>> requestHeaders)
method to fetch request
cookies from this cache. Unlike Java 5, Java 6 introduced a concrete implementation
of
CookieHandler
sothat HTTPprotocol handlers andapplications canworkwith
cookies.
The concrete
java.net.CookieManager
class extends
CookieHandler
to
manage cookies. A
CookieManager
object is initialized as follows:
• With a
cookie store
for storing cookies. The cookie store is based on the
java.net.CookieStore
interface.
• Witha
cookie policy
fordeterminingwhichcookiestoacceptforstorage.The
cookie policy is based on the
java.net.CookiePolicy
interface.
Create a cookie manager by calling either the
CookieManager()
constructor
or the
CookieManager(CookieStore store, CookiePolicy policy)
constructor. The
CookieManager()
constructor invokes the latter constructor with
null
arguments, using the default in-memory cookie store and the default accept-
cookies-from-the-original-server-onlycookiepolicy.Unlessyouplantocreateyourown
CookieStore
and
CookiePolicy
implementations, you'll most likely work with
the default constructor. The following example creates and establishes a new
Cook-
ieManager
object as the system-wide cookie handler: