public static CookieCollection GetCookieCollection()
        {
            var cookieHeader = CookieManager.Instance.GetCookie(digiview.Helper.SettingsHelper.GetPath());

            Android.Webkit.CookieManager cookieManager = Android.Webkit.CookieManager.Instance;
            cookieManager.SetAcceptCookie(true);
            string[] temp = cookieManager.GetCookie(digiview.Helper.SettingsHelper.GetPath()).Split(';');

            CookieCollection cookies = new CookieCollection();

            for (int i = 0; i < temp.Length; i++)
            {
                temp[i] = temp[i].Trim();
                string[] val = temp[i].Split('=');
                Cookie   c   = new Cookie();
                c.Name  = val[0];
                c.Value = val[1];
                for (int j = 2; j < val.Length; j++)
                {
                    c.Value += "=" + val[j];
                }
                c.Path = "/";
                cookies.Add(c);
            }

            return(cookies);
        }
示例#2
0
            public override void OnPageFinished(WebView view, string url)
            {
                if (Build.VERSION.SdkInt < Build.VERSION_CODES.Lollipop)
                {
                    CookieSyncManager.CreateInstance(context);
                }
                Android.Webkit.CookieManager cookieManager = CookieManager.Instance;
                string cookieStr = cookieManager.GetCookie("https://www.cnblogs.com/");

                if (string.IsNullOrWhiteSpace(cookieStr))
                {
                    return;
                }
                string[] cookieArray = cookieStr.Split(';');
                List <RestSharp.HttpCookie> cookieList = new List <RestSharp.HttpCookie>();

                foreach (var item in cookieArray)
                {
                    if (!string.IsNullOrWhiteSpace(item))
                    {
                        string cookieItemText       = item.Trim();
                        RestSharp.HttpCookie cookie = new RestSharp.HttpCookie();
                        int nameIndex = cookieItemText.IndexOf('=');
                        cookie.Name  = cookieItemText.Substring(0, nameIndex);
                        cookie.Value = cookieItemText.Substring(nameIndex + 1, cookieItemText.Length - nameIndex - 1);
                        cookieList.Add(cookie);
                    }
                }
                CookieSyncManager.Instance.Sync();
                List <HttpHeader> headers = new List <HttpHeader>();

                headers.Add(new HttpHeader()
                {
                    Name = "Host", Value = "www.cnblogs.com"
                });
                headers.Add(new HttpHeader()
                {
                    Name = "Origin", Value = "https://www.cnblogs.com"
                });
                headers.Add(new HttpHeader()
                {
                    Name = "X-Requested-With", Value = "XMLHttpRequest"
                });
                headers.Add(new HttpHeader()
                {
                    Name = "Referer", Value = "https://www.cnblogs.com/"
                });
                HttpClientUtil.PostWebHttp <WebResponseMessage, DiggBuryModel>("https://www.cnblogs.com/mvc/vote/VoteBlogPost.aspx", new DiggBuryModel("ansang", 9371764), headers, cookieList,
                                                                               (model) => {
                    System.Diagnostics.Debug.Write(model.Message);
                },
                                                                               (error) =>
                {
                    System.Diagnostics.Debug.Write(error);
                });
                base.OnPageFinished(view, url);
            }