Java Reference
In-Depth Information
CookieHandler.setDefault(new CookieManager());
Alongwiththeaforementionedconstructors,
CookieManager
declaresthefollow-
ing methods:
•
Map<String, List<String>> get(URI uri, Map<String,
List<String>> requestHeaders)
returns an immutable map of
Cookie
and
Cookie2
request headers for cookies obtained from the cookie
storewhosepathmatches
uri
'spath.Although
requestHeaders
isn'tused
by the default implementation of this method, it can be used by subclasses.
IOException
is thrown when an I/O error occurs.
•
CookieStore getCookieStore()
returnsthecookiemanager'scookie
store.
•
void put(URI uri, Map<String, List<String>> respon-
seHeaders)
stores all applicable cookies whose
Set-Cookie
and
Set-
Cookie2
response headers were retrieved from the specified
uri
value and
placed (with all other response headers) in the immutable
responseHead-
ers
mapinthecookiestore.
IOException
isthrownwhenanI/Oerroroc-
curs.
•
void setCookiePolicy(CookiePolicy cookiePolicy)
setsthe
cookie manager's cookie policy to one of
CookiePolicy.ACCEPT_ALL
(acceptallcookies),
CookiePolicy.ACCEPT_NONE
(acceptnocookies),or
CookiePolicy.ACCEPT_ORIGINAL_SERVER
(accept cookies from ori-
ginalserveronly—thisisthedefault).Passing
null
tothismethodhasnoef-
fect on the current policy.
In contrast to the
get()
and
put()
methods, which are called by HTTP protocol
handlers, an application works with the
getCookieStore()
and
setCook-
iePolicy()
methods. Consider
Listing 9-16
.
Listing 9-16.
Listing all cookies for a specific domain
import java.io.IOException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpCookie;