0

This question was asked before but not the correct working answer given.

Android - extracting cookies after login in webview

My question has many sub-parts: (visual studio 2017 Android Emulator API25)

1-I need to connect an url ../signin that redirects me to Office365 login. after that it will send me to ../login.

2- I need to get cookies that was given by office365/backend after the ../login redirection. (in iOS you dont need to declare the url to get the cookie, but in android you have to)(How do I access cookies in a Xamarin WebView?)

3- ../signin has HTTPS, but my webview element cannot open the website. However, I can open google, facebook etc. I recieve untrusted SSL error. from my laptop, I can connect to that url.

4- Although it is literally 2-3 lines of code for iOS, for android I couldnt find any result. I need to get current url of webview after redirections. However, webview.URL only returns the initial given url of webview.loadURl(String url), although it is not the current url.

5- I searched for webview.onPageFinished(WebView webview,String URL) in order to put current url and check if it is ../login rightnow. So I can get the cookies. iOS has onPageFinished event listener, but Android has not.

Heres my code for the thing:

        WebView wv = new WebView(this);
        SetContentView(wv);
        wv.SetWebViewClient(new WebViewClient());
        wv.LoadUrl("../signin");
        wv.Settings.JavaScriptEnabled = true;

        var client = new WebViewClient();
        wv.SetWebViewClient(new WvClient(this, wv));

for onPageListener

    public class WvClient : WebViewClient
{
    public Activity mActivity;
    public WvClient(Activity mActivity,WebView wv)
    {
        this.mActivity = mActivity;
        onPageFinished(wv,wv.Url);
    }
    public void onPageFinished(WebView wv, String url)
    {


        var cookieHeader = CookieManager.Instance.GetCookie(wv.Url);//This is how I will get the cookie.
    }

    public void onReceivedSslError(WebView view, SslErrorHandler handler, Android.Net.Http.SslError er)
    {
        handler.Proceed();// Ignore SSL certificate errors
    }
}

Thank you!!

CSSguy
  • 5
  • 4

1 Answers1

0

You should override these method. Then you could each url of webview after redirections.

For example:

        public override void OnPageFinished(WebView view, string url)
        {              
            var cookieHeader = CookieManager.Instance.GetCookie(view.Url);
            base.OnPageFinished(view, url);
        }

        public override void OnReceivedSslError(WebView view, SslErrorHandler handler, SslError error)
        {
            handler.Proceed();
        }
Billy Liu - MSFT
  • 2,168
  • 1
  • 8
  • 15
  • Thanks for the help. It was flagging no method to override etc, but realized it should be OnPageFinished, instead of onPageFinished.. Well.. – CSSguy Mar 29 '18 at 14:56