-1

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.

Paul Reiners
  • 8,896
  • 30
  • 107
  • 178
  • 2
    duplicate http://stackoverflow.com/questions/531393/how-to-detect-if-cookies-are-disabled-is-it-possible – Bozho Feb 10 '10 at 21:27
  • on second thought - might not be a duplicate, if it regards HttpURLConnection. So - what do you mean by JavaSE? Is it about checking for cocokies with an applet? HttpURLConenction? – Bozho Feb 10 '10 at 21:34
  • 1
    Please elaborate more about the context of this question. Does this Java code run at client machine or server machine? It sounds like that you want to run it at the very same client machine and test if the client's default browser has cookies enabled, is this true? – BalusC Feb 10 '10 at 21:35
  • HttpURLConnection makes a request, doesn't listen for one. – Murali VP Feb 10 '10 at 21:36
  • Java Platform, Standard Edition (Java SE) lets you develop and deploy Java applications on desktops and servers, as well as today's demanding Embedded and Real-Time environments. – Paul Reiners Feb 10 '10 at 21:48

3 Answers3

2

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
  • 5,549
  • 4
  • 25
  • 35
0

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
  • 632,615
  • 86
  • 730
  • 1,096
0

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.

Lawrence Dol
  • 59,198
  • 25
  • 134
  • 183