Java Reference
In-Depth Information
import java.net.URL;
import java.util.List;
class ListAllCookies
{
public static void main(String[] args) throws IOExcep-
tion
{
if (args.length != 1)
{
System.err.println("usage: java ListAllCookies
url");
return;
}
CookieManager cm = new CookieManager();
cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cm);
new URL(args[0]).openConnection().getContent();
List<HttpCookie>
cookies
=
cm.getCookieStore().getCookies();
for (HttpCookie cookie: cookies)
{
System.out.println("Name = "+cookie.getName());
System.out.println("Value
=
"+cook-
ie.getValue());
System.out.println("Lifetime
(seconds)
=
"+cookie.getMaxAge());
System.out.println("Path = "+cookie.getPath());
System.out.println();
}
}
}
Listing 9-16 describes a command-line application that obtains and lists all cookies
from its single domain-name argument.
Search WWH ::




Custom Search