Java Reference
In-Depth Information
@Override
public
boolean
shouldAccept
(
URI
uri
,
HttpCookie
cookie
)
{
if
(
uri
.
getAuthority
().
toLowerCase
().
endsWith
(
".gov"
)
||
cookie
.
getDomain
().
toLowerCase
().
endsWith
(
".gov"
))
{
return
false
;
}
return
true
;
}
}
CookieStore
It is sometimes necessary to put and get cookies locally. For instance, when an applica‐
tion quits, it can save the cookie store to disk and load those cookies again when it next
starts up. You can retrieve the store in which the
CookieManager
saves its cookies with
the
getCookieStore()
method:
CookieStore
store
=
manager
.
getCookieStore
();
The
CookieStore
class allows you to add, remove, and list cookies so you can control
the cookies that are sent outside the normal flow of HTTP requests and responses:
public
void
add
(
URI
uri
,
HttpCookie
cookie
)
public
List
<
HttpCookie
>
get
(
URI
uri
)
public
List
<
HttpCookie
>
getCookies
()
public
List
<
URI
>
getURIs
()
public
boolean
remove
(
URI
uri
,
HttpCookie
cookie
)
public
boolean
removeAll
()
Each cookie in the store is encapsulated in an
HttpCookie
object that provides methods
for inspecting the attributes of the cookie summarized in
Example 6-2
.
Example 6-2. The HTTPCookie class
package
java
.
net
;
public
class
HttpCookie
implements
Cloneable
{
public
HttpCookie
(
String
name
,
String
value
)
public
boolean
hasExpired
()
public
void
setComment
(
String
comment
)
public
String
getComment
()
public
void
setCommentURL
(
String
url
)
public
String
getCommentURL
()
public
void
setDiscard
(
boolean
discard
)
public
boolean
getDiscard
()
public
void
setPortlist
(
String
ports
)
public
String
getPortlist
()
public
void
setDomain
(
String
domain
)
public
String
getDomain
()
public
void
setMaxAge
(
long
expiry
)
public
long
getMaxAge
()