Exemplo n.º 1
0
        public CookieState this[string name]
        {
            get
            {
                if (String.IsNullOrEmpty(name))
                {
                    return(null);
                }

                CookieState cookie = Cookies.FirstOrDefault(c => String.Equals(c.Name, name, StringComparison.OrdinalIgnoreCase));
                if (cookie == null)
                {
                    cookie = new CookieState(name, String.Empty);
                    Cookies.Add(cookie);
                }
                return(cookie);
            }
        }
Exemplo n.º 2
0
        private async void OnGetValuesCommand()
        {
            string uri = $"{DefaultApiUrl}values";

            HttpCookie blog = Cookies.FirstOrDefault(c => c.Name == "custom_blog");

            if (blog != null)
            {
                blog.Value = "http://kaki104.tistory.com";
                _httpFilter.CookieManager.SetCookie(blog);
            }
            else
            {
                var existCookie = Cookies.FirstOrDefault();
                if (existCookie == null)
                {
                    return;
                }
                var cookie = new HttpCookie("custom_blog", existCookie.Domain, existCookie.Path)
                {
                    Value = "http://kakisoft.com"
                };
                _httpFilter.CookieManager
                .SetCookie(cookie);
            }

            using (HttpClient client = new HttpClient())
            {
                HttpResponseMessage response = await client.GetAsync(new Uri(uri));

                if (response.IsSuccessStatusCode == false)
                {
                    return;
                }

                string result = await response.Content.ReadAsStringAsync();

                Debug.WriteLine(result);
            }
        }
Exemplo n.º 3
0
        public static Cookie GetResponseCookie(HttpResponseMessage response)
        {
            if (response == null)
            {
                throw new ArgumentNullException("response", "The http response message to get the cookie from cannot be null.");
            }

            if (response.IsSuccessStatusCode)
            {
                IEnumerable <string> Cookies;

                /*
                 * Cookie string
                 * ibapauth="group=admin-group,ctime=1495590270,ip=192.168.1.101,auth=LOCAL,client=API,su=1,timeout=600,mtime=1495591213,user=admin,2CF4756i4DZ3lQ1rBn8t9epWMHYcWZ1TogQ"; httponly; Path=/; secure
                 */
                response.Headers.TryGetValues("Set-Cookie", out Cookies);

                if (Cookies != null && Cookies.Any())
                {
                    string CookieString = Cookies.FirstOrDefault(x => x.StartsWith("ibapauth"));

                    if (!String.IsNullOrEmpty(CookieString))
                    {
                        IEnumerable <string> CookieParts = CookieString.Split(';').Select(x => { return(x.Trim()); });

                        if (CookieParts.Any())
                        {
                            if (!String.IsNullOrEmpty(CookieParts.First()))
                            {
                                Cookie Cookie = new Cookie("ibapauth", CookieParts.First().Replace("ibapauth=", ""), "", response.RequestMessage.RequestUri.Host);

                                if (!String.IsNullOrEmpty(CookieParts.FirstOrDefault(x => x.Equals("secure", StringComparison.OrdinalIgnoreCase))))
                                {
                                    Cookie.Secure = true;
                                }

                                if (!String.IsNullOrEmpty(CookieParts.FirstOrDefault(x => x.Equals("httponly", StringComparison.OrdinalIgnoreCase))))
                                {
                                    Cookie.HttpOnly = true;
                                }

                                string[] ValueParts       = CookieParts.First().Split(',');
                                string   TimeoutString    = ValueParts.FirstOrDefault(x => x.StartsWith("timeout=", StringComparison.OrdinalIgnoreCase));
                                string   ModifyTimeString = ValueParts.FirstOrDefault(x => x.StartsWith("mtime=", StringComparison.OrdinalIgnoreCase));

                                if (!String.IsNullOrEmpty(TimeoutString) && !String.IsNullOrEmpty(ModifyTimeString))
                                {
                                    try
                                    {
                                        string[] TimeoutParts = TimeoutString.Split('=');

                                        if (TimeoutParts.Length == 2)
                                        {
                                            int Timeout;
                                            if (Int32.TryParse(TimeoutParts[1], out Timeout))
                                            {
                                                string[] ModifyTimeParts = ModifyTimeString.Split('=');

                                                if (ModifyTimeParts.Length == 2)
                                                {
                                                    int Modify;

                                                    if (Int32.TryParse(ModifyTimeParts[1], out Modify))
                                                    {
                                                        Cookie.Expires = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(Modify).AddSeconds(Timeout);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    catch (Exception)
                                    {
                                    }
                                }

                                return(Cookie);
                            }
                        }
                    }
                }
            }

            return(null);
        }