Unfortunately, there's a multitude of cookie managers for Android. The cookies for HttpURLConnection
are maintained by java.net.CookieManager
and the cookies for WebView
are maintained by android.webkit.CookieManager
. These cookie repositories are separate and require manual synchronization.
My app uses both HttpURLConnections
and shows WebViews
(it's a native-HTML hybrid). Naturally, I want both to share all cookies - so I will have a transparent session all across.
More Specifically:
- When a cookie is set/changed in an HttpURLConnection, I want the WebViews to see this change as well.
- When a cookie is set/changed in a WebView, I want the next HttpURLConnections to see this change as well.
Simply put - I'm looking for a two-way sync. Or even better, to have them both use the same cookie repository. You can assume both are active in the same time (like on different tabs).
Questions:
Is there a way to make both use the same cookie repository?
If not, what is the recommended practice to do the manual sync? When exactly should I sync and how?
Related Question: This question tackles a similar issue, but only implements one-way sync (HttpURLConnection -> WebView).
My Best Idea So Far: I really want to avoid a manual sync, so I tried to think how to make both use the same repository. Maybe I can create my own core handler which extends java.net.CookieManager
. I will set it as the core cookie handler using java.net.CookieHandler.setDefault()
. Its implementation will be a proxy to the android.webkit.CookieManager
handler instance (for every function I'll simply access the webkit manager).