I have a web application that makes HTTP requests using HttpURLConnection
. I need it to handle cookies. I know that it's easily done by adding just one line of code, something like
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER));
The problem is this way I'm setting the system-wide cookie handler as the documentation describes. This also affects other web applications that run in the same servlet container. For example if I want CookiePolicy.ACCEPT_ORIGINAL_SERVER
in one application and CookiePolicy.ACCEPT_ALL
in another, it won't work.
Is there a way to have a CookieHandler
that is only used by a single HttpURLConnection
instance?
In standard oracle implementation the
HttpURLConnection
get the defaultCookieHandler
on the constructor, so this is one possible solution. Create a synchronized singleton factory that create theHttpURLConnections
using a specific manager for each application. Not good idea in my opinion.Other bad idea is provide your own
CookiePolicy
and do the trick on theshouldAccept
method.Or you can manually control cookies on the app that should not share the
CookieHandler
: