Ejemplo n.º 1
0
        internal static void Set(HTTPResponse response)
        {
            if (response == null)
            {
                return;
            }
            object locker = CookieJar.Locker;

            lock (locker)
            {
                try
                {
                    CookieJar.Maintain();
                    List <Cookie> list         = new List <Cookie>();
                    List <string> headerValues = response.GetHeaderValues("set-cookie");
                    if (headerValues != null)
                    {
                        foreach (string current in headerValues)
                        {
                            try
                            {
                                Cookie cookie = Cookie.Parse(current, response.baseRequest.CurrentUri);
                                if (cookie != null)
                                {
                                    int    num;
                                    Cookie cookie2 = CookieJar.Find(cookie, out num);
                                    if (!string.IsNullOrEmpty(cookie.Value) && cookie.WillExpireInTheFuture())
                                    {
                                        if (cookie2 == null)
                                        {
                                            CookieJar.Cookies.Add(cookie);
                                            list.Add(cookie);
                                        }
                                        else
                                        {
                                            cookie.Date            = cookie2.Date;
                                            CookieJar.Cookies[num] = cookie;
                                            list.Add(cookie);
                                        }
                                    }
                                    else if (num != -1)
                                    {
                                        CookieJar.Cookies.RemoveAt(num);
                                    }
                                }
                            }
                            catch
                            {
                            }
                        }
                        response.Cookies = list;
                    }
                }
                catch
                {
                }
            }
        }
Ejemplo n.º 2
0
        internal static void Persist()
        {
            object locker = CookieJar.Locker;

            lock (locker)
            {
                try
                {
                    CookieJar.Maintain();
                    if (!Directory.Exists(CookieJar.CookieFolder))
                    {
                        Directory.CreateDirectory(CookieJar.CookieFolder);
                    }
                    using (FileStream fileStream = new FileStream(CookieJar.LibraryPath, FileMode.Create))
                    {
                        using (BinaryWriter binaryWriter = new BinaryWriter(fileStream))
                        {
                            binaryWriter.Write(1);
                            int num = 0;
                            foreach (Cookie current in CookieJar.Cookies)
                            {
                                if (!current.IsSession)
                                {
                                    num++;
                                }
                            }
                            binaryWriter.Write(num);
                            foreach (Cookie current2 in CookieJar.Cookies)
                            {
                                if (!current2.IsSession)
                                {
                                    current2.SaveTo(binaryWriter);
                                }
                            }
                        }
                    }
                }
                catch
                {
                }
            }
        }