views:

346

answers:

3

I want to delete cookies programmatically.

More specifically, I am using HtmlUnit with Java to automate browser operations. After performing some operations I want to clear cookies so that my subsequent operations make sense. How can I clear cookies through HtmlUnit or through Java or through any other way automatically.

+1  A: 

Can you use the CookieManager?

This class appears to have methods for clearing all cookies or removing them individually.

Drew Wills
I will look at it. Thanks for the answer.
Yatendra Goel
+1  A: 

You can user javascript code to expire your cookie using Now to expire it immediately

document.cookie = name + "=; " + Now + "; path=/";

syntax for creating cookie

document.cookie = "name=value; expires=date; path=path; domain=domain; secure";

Ravia
Do you mean `document.cookie = name + "=; expires=" + Now + "; path=/";`?
PP
how can I give a command from java to execute javascript code... I mean how can integrate the javascript code with my existing java code
Yatendra Goel
You have to write it within script tag which gets rendered on client side. Response.write("<script>document.cookie = name + "=; expires=" + Now + "; path=/";</script>");
Ravia
A: 

In actual, the cookies in HtmlUnit doesn't get stored on disk until saved programmatically. They remain in the memory. From the memory they gets deleted when the HtmlUnit session expires.

Yatendra Goel