I'm setting two cookies using setCookie method from android.webkit.CookieManager - https://developer.android.com/reference/android/webkit/CookieManager.html with the same value for two different URLs.
However, I know that when I load the first URL on the webview, it will send me a HTTP redirect to the second URL for which I've also setted the cookie.
My question is: will the cookie manager send the cookie for the second URL?
Yes.
As long as a cookie fulfills the requirements (domain, path, secure, httponly, not expired, etc.) then the WebView will send the cookie along with each request. This includes when the WebView makes a request for a redirect URL, if there are cookies that fulfill the requirements for the redirected URL then the WebView will send those cookies along with the request. So, if you explicitly set a cookie for the redirect URL then it should be included when the WebView follows the redirect and makes a request for the redirect URL.
Example 1
Use
android.webkit.CookieManager
to set the cookies that allWebView
instances will use. I typically do this in my Activity'sonCreate()
method, or my Fragment'sonViewCreated()
method, but you can configure theCookieManager
in almost any lifecycle method, but it must be done before theWebView
loads the url. This is an example of configuring theCookieManager
inonViewCreated()
.Example 2
If you know that the redirect URL will be in the same apex domain, ex.
mydomain.com
will redirect toredirect.mydomain.com
, orwww.mydomain.com
will redirect tosubdomain.mydomain.com
, orsubdomain.mydomain.com
will redirect tomydomain.com
, then you can set one cookie for the entire mydomain.com domain.