Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView losing Cookies in redirects

My problem is with WebView dealing with redirects and custom cookies.

I do following:

  1. Create instances of CookieSyncManager and CookieManager in application create
  2. Call sCookieManager.setAcceptCookie(true); in static section in util class
  3. Call sCookieManager.setCookie(url, value); sCookieSyncManager.sync();
  4. And finally mWebView.loadUrl

What's happening in server side is that it first gets the correct cookie I have set, runs some redirect and cookie seems to be lost. Any ideas, what I am doing wrong here?

I've tried running setCookie - sync in another thread giving some delay and then loadUrl but it didn't help.

All similar posts seem to have solutions which are not working.

Thanks.

UPDATE:

I noticed that using setCookie to give multiple Cookies like for example:

setCookie("MyCookie=value; Domain=mydomain.com; Path=/; Secure; HttpOnly; MySecondCookie=value2....)"

Only the first one is applied, and then it's gone when server runs redirects on it's own domain. This issue can be fixed settings custom cookies one in a time in a loop.

I have temporarily added handler.proceed(); in onReceivedSslError and I can see from the logs that it's triggering just before web site redirects and Cookie is lost in on next pages onPageFinished, could there be some connection between secure cookies and invalid certificate chains?

like image 730
Niko Avatar asked Dec 09 '13 12:12

Niko


People also ask

How do I enable cookies on Android WebView?

How do I enable cookies in a webview? CookieManager. getInstance(). setAcceptCookie(true);

How do I set cookies in WebView?

It's quite simple really. String cookieString = "cookie_name=cookie_value; path=/"; CookieManager. getInstance(). setCookie(baseUrl, cookieString);

Do Webviews share cookies?

Thankfully, UWP and iOS share their cookie containers automatically between the WebView and native http client, however Android does not.

How to go back beyond url a in WebView?

URL A, URL B ] On back key click from URL B webpage, webview will try to load URL A, which again redirects to URL B. We need to double click back key twice rapidly, to go back beyond URL A

What happens when you click on a link in WebView?

And i want to allow it? According to the official documentation, a click on any link in WebView launches an application that handles URLs, which by default is a browser. You need to override the default behavior like this

How to make the WebView handle URLs loaded by the user?

Even better just call myWebView.setWebViewClient (new WebViewClient ()); The default implementation of shouldOverrideUrlLoading returns false. Just adding a default custom WebViewClient will do. This makes the WebView handle any loaded urls itself.


Video Answer


1 Answers

Try using not

sCookieManager.setCookie(url, value) 

but

sCookieManager.setCookie(cookieDomain, value)

cookieDomain you can find for example using chrome://inspect

Working example:

String cookieDomain = ".www.drive2.ru";
String siteUrl = "https://drive2.ru";

webView = (WebView) getView().findViewById(R.id.web_view);
webView.setWebViewClient(new WebViewClient()); // force open any new url in same webview (whether it is user click or redirect)

CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie(cookieDomain, "cookieName=cookieValue");
webView.loadUrl(siteUrl);
like image 197
Deepscorn Avatar answered Oct 07 '22 18:10

Deepscorn