0

I want to get the cookie value sent from a web browser from a Java application.

The cookie is not sent in response headers from the WebServer. I think the cookie is being sent as a Session cookie - which I am not able to get the cookie value in Java application.

I tried this:

             CookieHandler handler = CookieHandler.getDefault();
             if (handler != null)    {
                  Map<String, List<String>> headers = handler.get(url.toURI(), new HashMap<String, List<String>>());
                  List<String> values = headers.get("Cookie");
                  for (Iterator<String> iter=values.iterator(); iter.hasNext();) {
                       String v = iter.next(); 

                       if (cookieValue == null) 
                            cookieValue = v; 
                       else
                            cookieValue = cookieValue + ";" + v; 
                  } 
             }

but my efforts in vain, I am not able to get the cookie. I tried simply typing the URL in the browser, and I am able to see the 'Cookie' in the browser, but I am not able to get the same cookie from the java program - any help could be greatly helpful


Thanks BalusC, I will explain the issue.

I was actually looking out for a Cookie from Google Analytics and the Cookie is of the form "__gads:ID=blah".

This Cookie is sent to the Client only for the first time and if it already exists - Google Analytics wont send the Cookie again (all these days I am Checking for this Cookie)

Now my question where can I find this Cookie, which should be already existing in my system. Any thoughts?

Kev
  • 118,037
  • 53
  • 300
  • 385

1 Answers1

3

This won't work. The java.net.CookieHandler is completely unrelated to the webbrowser-specific cookie stores. The CookieHandler is only been used when java.net.URLConnection is been used to programmatically fire HTTP requests using Java language (see also this mini tutorial)

If you intend to access a webbrowser-specific cookie store, then you'd have to use their Java API -if any provided by the particular webbrowser make-, or to look/guess their location at the disk file system and/or in the platform-specific registry, depending on the webbrowser make/version and the platform used.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks BalusC, but I am sorry I think I haven't asked the question correctly. I did a http request to the server (via URL.openConnection()) and used the above code to get the cookies, but I could always see that no cookies are returned. But when I requested the same server from the web browser I could see the cookies (I saw this from the Developer tools in Chrome). I dont know what mistake I did. :( – Praveen Kondapalli Jul 05 '12 at 12:11
  • Click the "mini tutorial" link. – BalusC Jul 05 '12 at 18:23