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 all WebView
instances will use. I typically do this in my Activity's onCreate()
method, or my Fragment's onViewCreated()
method, but you can configure the CookieManager
in almost any lifecycle method, but it must be done before the WebView
loads the url. This is an example of configuring the CookieManager
in onViewCreated()
.
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//Replace R.id.webview with whatever ID you assigned to the WebView in your layout
WebView webView = view.findViewById(R.id.webview);
CookieManager cookieManager = CookieManager.getInstance();
//originalUrl is the URL you expect to generate a redirect
cookieManager.setCookie(originalUrl, "cookieKey=cookieValue");
//redirectUrl is the URL you expect to be redirected to
cookieManager.setCookie(redirectUrl, "cookieKey=cookieValue");
//Now have webView load the originalUrl. The WebView will automatically follow the redirect to redirectUrl and the CookieManager will provide all cookies that qualify for redirectUrl.
webView.loadUrl(originalUrl);
}
Example 2
If you know that the redirect URL will be in the same apex domain, ex. mydomain.com
will redirect to redirect.mydomain.com
, or www.mydomain.com
will redirect to subdomain.mydomain.com
, or subdomain.mydomain.com
will redirect to mydomain.com
, then you can set one cookie for the entire mydomain.com domain.
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//Replace R.id.webview with whatever ID you assigned to the WebView in your layout
WebView webView = view.findViewById(R.id.webview);
CookieManager cookieManager = CookieManager.getInstance();
//Set the cookie for the entire domain - notice the use of a . ("dot") at the front of the domain
cookieManager.setCookie(".mydomain.com", "cookieKey=cookieValue");
//Now have webView load the original URL. The WebView will automatically follow the redirect to redirectUrl and the CookieManager will provide all cookies for the domain.
webView.loadUrl(originalUrl);
}