Reputation: 307
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?
Upvotes: 0
Views: 5845
Reputation: 1108722
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.
Upvotes: 3