Exemplo n.º 1
0
        public void OnAfterRequestSent(WS.HttpRequestData data)
        {
            if (!CookiesByThread.ContainsKey(Thread.CurrentThread.ManagedThreadId))
            {
                CookiesByThread.Add(Thread.CurrentThread.ManagedThreadId, new Dictionary <string, string>());
            }

            var cookies = CookiesByThread[Thread.CurrentThread.ManagedThreadId];

            string cookie = data.GetResponseHeader("Set-Cookie");

            if (!string.IsNullOrEmpty(cookie))
            {
                Uri uri = new Uri(data.Url);

                if (cookies.ContainsKey(uri.Host))
                {
                    cookies[uri.Host] = cookie;
                }
                else
                {
                    cookies.Add(uri.Host, cookie);
                }
            }
        }
Exemplo n.º 2
0
        public bool OnBeforeRequestSent(WS.HttpRequestData data)
        {
            if (!CookiesByThread.ContainsKey(Thread.CurrentThread.ManagedThreadId))
            {
                CookiesByThread.Add(Thread.CurrentThread.ManagedThreadId, new Dictionary <string, string>());
            }

            var cookies = CookiesByThread[Thread.CurrentThread.ManagedThreadId];

            Uri uri = new Uri(data.Url);

            if (cookies.ContainsKey(uri.Host))
            {
                var cookie = data.Headers?.FirstOrDefault(h => h.Name == "Cookie");
                if (cookie != null)
                {
                    cookie.Value = cookies[uri.Host];
                }
                else
                {
                    data.Headers.Add(new WS.HttpRequestHeader()
                    {
                        Name = "Cookie", Value = cookies[uri.Host]
                    });
                }
            }
            else
            {
                var cookie = data.Headers?.FirstOrDefault(h => h.Name == "Cookie");
                if (cookie != null)
                {
                    data.Headers.Remove(cookie);
                }
            }

            return(true);
        }