Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Read cookies from PCL

Hello
I would like to read cookie value, which is set by javascript in a webview
The webview is implemented and managed within PCL project
The following code is executed without errors but allows to get only an empty object CookieCollection
Can you please help?

private void webView_Navigated(object sender, WebNavigatedEventArgs e)
        {
            if (e.Result == WebNavigationResult.Success)
            {
                try
                {
                    var assembly = typeof(App).GetTypeInfo().Assembly;
                    Stream stream = assembly.GetManifestResourceStream("script.js");
                    string script = "";
                    using (var reader = new StreamReader(stream))
                    {
                        script = reader.ReadToEnd();
                    }
                    webView.Eval(script);
                    string url = ((UrlWebViewSource)webView.Source).Url;
                    var handler = new HttpClientHandler { CookieContainer = new CookieContainer() };
                    var httpClient = new HttpClient(handler);
                    CookieCollection Cookies = handler.CookieContainer.GetCookies(new Uri(url));
                    if (Cookies.Count > 0)
                    {
                        string value = Cookies["location"].Value;
                    }
                }
                catch (Exception ex)
                {
                    string message = ex.Message;
                }
            }
        }

Best Answer

Answers

Sign In or Register to comment.