Beispiel #1
0
 /// <summary>
 /// Will add a new, or overwrite an old cookie if already exists.
 /// </summary>
 public static void Set(Uri uri, Cookie cookie)
 {
     Set(cookie);
 }
Beispiel #2
0
        /// <summary>
        /// Will set or update all cookies from the response object.
        /// </summary>
        internal static void Set(HTTPResponse response)
        {
            if (response == null)
            {
                return;
            }

            lock (Locker)
            {
                try
                {
                    Maintain();

                    List <Cookie> newCookies       = new List <Cookie>();
                    var           setCookieHeaders = response.GetHeaderValues("set-cookie");

                    // No cookies. :'(
                    if (setCookieHeaders == null)
                    {
                        return;
                    }

                    foreach (var cookieHeader in setCookieHeaders)
                    {
                        try
                        {
                            Cookie cookie = Cookie.Parse(cookieHeader, response.baseRequest.CurrentUri);

                            if (cookie != null)
                            {
                                int idx;
                                var old = Find(cookie, out idx);

                                // if no value for the cookie or already expired then the server asked us to delete the cookie
                                bool expired = string.IsNullOrEmpty(cookie.Value) || !cookie.WillExpireInTheFuture();

                                if (!expired)
                                {
                                    // no old cookie, add it straith to the list
                                    if (old == null)
                                    {
                                        Cookies.Add(cookie);

                                        newCookies.Add(cookie);
                                    }
                                    else
                                    {
                                        // Update the creation-time of the newly created cookie to match the creation-time of the old-cookie.
                                        cookie.Date  = old.Date;
                                        Cookies[idx] = cookie;

                                        newCookies.Add(cookie);
                                    }
                                }
                                else if (idx != -1) // delete the cookie
                                {
                                    Cookies.RemoveAt(idx);
                                }
                            }
                        }
                        catch
                        {
                            // Ignore cookie on error
                        }
                    }

                    response.Cookies = newCookies;
                }
                catch
                { }
            }
        }