My problem is with WebView dealing with redirects and custom cookies.
I do following:
CookieSyncManager and CookieManager in application createsCookieManager.setAcceptCookie(true); in static section in util classsCookieManager.setCookie(url, value);
sCookieSyncManager.sync();
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?
How do I enable cookies in a webview? CookieManager. getInstance(). setAcceptCookie(true);
It's quite simple really. String cookieString = "cookie_name=cookie_value; path=/"; CookieManager. getInstance(). setCookie(baseUrl, cookieString);
Thankfully, UWP and iOS share their cookie containers automatically between the WebView and native http client, however Android does not.
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
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
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With