views:

284

answers:

3

Is it possible in Java (SE, not J2EE) to detect whether cookies are enabled in the default browser?

This isn't a client/server application. It's simply a desktop application that happens to interact with the default browser.

+2  A: 

I am not sure what you mean by "default browser" but the fundamental thing to understand here is that the code, be it written in Java, C++, Python, you name it, that handles a HTTP request should set a cookie using Set-Cookie and look for what was set being available in Cookie header in the subsequent requests from the browser. If cookies are turned off in the client, then there won't be a Cookie header in the subsequent requests. A simple redirect to the initial request can also be used to generate a "subsequent" request.

Murali VP
Your default browser is the browser that the OS looks to first when opening an internet link.
Paul Reiners
A: 

There are many browsers out there, and each one uses a different mechanism for storing the user's preferences ... including the preferences that determine whether the user has disabled cookies.

You could conceivably implement a Java application that knew how to unpick the settings information for some popular browsers.

But this would contain a lot of browser specific and operating system specific code. And in the case of Windows, it would entail grovelling around in the registry; e.g. using some third party library. And of course, this would all be rather fragile because:

  1. the next version/release of the user's browser could use a different settings representation,

  2. the user could use some new browser you've never heard of,

  3. different Linux distros, etc could be configured to use different default locations for settings files, and/or the preference file that says what the user's default browser is, and

  4. the user could override the default locations anyway, in ways that a generic desktop application could not fathom.

EDIT - on the other hand, if your desktop application (acting in the server role of the HTTP protocol) simply needs to figure out if the browser it is currently talking to will allow it to set cookies, the simple solution is to try to do it and see if it works.

Finally, you need to bear in mind that the user may not be using the default browser for whatever it is he is trying to do. I frequently have multiple browsers installed on a machine, and may use the non-default one for certain things.

Stephen C
A: 

Assuming your application is acting as a localhost server to the browser, which is what your question seems to say...

One way is to redirect the request to a special URL with a special cookie, say "CookieCheck" set to some known special value and the original URL encoded as query data. When a request is received for the special URL, if "CookieCheck" is received then the browser accepted the cookie - you can then redirect back to the original URL but with the cookie set to a real value.

This can be difficult to do for POST requests unless you can store the post data server side or encode it into query data. It also transforms the request from POST to GET, which can be problematic.

Software Monkey