Exemple #1
0
        public CookieCollection GetCookies(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            CheckExpiration();
            CookieCollection coll = new CookieCollection();

            if (cookies == null)
            {
                return(coll);
            }

            foreach (Cookie cookie in cookies)
            {
                string domain = cookie.Domain;
                string host   = uri.Host;
                if (!CheckDomain(domain, host))
                {
                    continue;
                }

                if (cookie.Port != "" && cookie.Ports != null && uri.Port != -1)
                {
                    if (Array.IndexOf(cookie.Ports, uri.Port) == -1)
                    {
                        continue;
                    }
                }

                string path    = cookie.Path;
                string uripath = uri.AbsolutePath;
                if (path != "" && path != "/")
                {
                    if (uripath != path)
                    {
                        if (!uripath.StartsWith(path))
                        {
                            continue;
                        }

                        if (path [path.Length - 1] != '/' && uripath.Length > path.Length &&
                            uripath [path.Length] != '/')
                        {
                            continue;
                        }
                    }
                }

                if (cookie.Secure && uri.Scheme != "https")
                {
                    continue;
                }

                coll.Add(cookie);
            }

            coll.SortByPath();
            return(coll);
        }