1

Could someone tell me how to delete the cookie that is either in memory of on hardrive in java on the client side? Just like how browser deletes all the cookies and session information.

I'm trying to do some PoC's for my work and I'm using simple apache Http classes for sending requests and then passing cookies in multiple requests but what if let's say I want to delete my session that is stored in the cookie.

I think this can be done because all browsers let you do this.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Dashing Boy
  • 477
  • 2
  • 10
  • 21
  • I wasn't aware that HttpClient stored it's Cookies anywhere other then memory. Id' take a look at the [CookieStore](http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/CookieStore.html) for some more details – MadProgrammer Jul 23 '13 at 11:15
  • Please any help would be greatly appreciated. – Dashing Boy Jul 23 '13 at 14:02

1 Answers1

1

Cookies (client side) are the equivalent of sessions (server side). I don't think there is a way to force the browser to delete the cookie, but you can suggest it to do so by:

Ending the session on server side:

HttpSession session = request.getSession();
session.invalidate();

or setting a short session period of timeout:

HttpSession session = request.getSession();
session.setMaxInactiveInterval(1*60); //in seconds
Daniel Gabado
  • 284
  • 1
  • 6
  • Yeah, that I know but I specifically talking about the client side. I mean when you close the firefox and I have setup to delete cookies when I close the browser. How could I replicate the same functionality in code. Say I'm sending requests to my server from another machine is there a way to let the server to forcefully close the session or delete the cookie from the client side. How could I replicate this functionality in HttpClient? Setting expiry date didn't work out. – Dashing Boy Jul 23 '13 at 14:01
  • Try setting the cookie's expire date to right now or equals 0. See [this question](http://stackoverflow.com/questions/252660/how-can-i-delete-my-browser-cookies-using-javascript) for some enlightment on the subject (in javascript). But there is no reliable way (see [this answer](http://stackoverflow.com/questions/752197/should-a-web-browser-delete-all-session-expiry-0-cookies-on-exit?rq=1)). Why is it important that the cookie gets deleted? – Daniel Gabado Jul 24 '13 at 17:34