Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

Read cookies from PCL

$
0
0

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;
                }
            }
        }

Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>