Ejemplo n.º 1
0
            protected override WebResponse GetWebResponse(WebRequest request)
            {
                var credentials = request.Credentials;

                if (request.RequestUri.Scheme == "http")
                {
                    request.Credentials = null;
                }
                string setCookieHeader = null;
                string host;

                try
                {
                    return(base.GetWebResponse(request));
                }
                catch (WebException e)
                {
                    if (e.Response == null || (((HttpWebResponse)e.Response).StatusCode != HttpStatusCode.Unauthorized))
                    {
                        throw;
                    }
                    if (!(request is RecreatableWebRequest))
                    {
                        throw;
                    }
                    setCookieHeader = e.Response.Headers[HttpResponseHeader.SetCookie];
                    host            = ((HttpWebResponse)e.Response).ResponseUri.Host;
                    ((RecreatableWebRequest)request).Recreate();
                    request.Credentials = credentials;
                }
                if (setCookieHeader != null)
                {
                    _container.Add(CookieParser.ParseCookie(setCookieHeader, host));
                }
                return(base.GetWebResponse(request));
            }
Ejemplo n.º 2
0
        public static Cookie ParseCookie(string header, string host)
        {
            string       name, val;
            Cookie       cookie = null;
            CookieParser parser = new CookieParser(header);

            while (parser.GetNextNameValue(out name, out val))
            {
                if ((name == null || name == "") && cookie == null)
                {
                    continue;
                }

                if (cookie == null)
                {
                    cookie = new Cookie(name, val);
                    continue;
                }

                name = name.ToUpper();
                switch (name)
                {
                case "COMMENT":
                    if (cookie.Comment == null)
                    {
                        cookie.Comment = val;
                    }
                    break;

                case "COMMENTURL":
                    if (cookie.CommentUri == null)
                    {
                        cookie.CommentUri = new Uri(val);
                    }
                    break;

                case "DISCARD":
                    cookie.Discard = true;
                    break;

                case "DOMAIN":
                    if (cookie.Domain == "")
                    {
                        cookie.Domain = val;
                    }
                    break;

                case "HTTPONLY":
                    cookie.HttpOnly = true;
                    break;

                case "MAX-AGE":     // RFC Style Set-Cookie2
                    if (cookie.Expires == DateTime.MinValue)
                    {
                        try
                        {
                            cookie.Expires = cookie.TimeStamp.AddSeconds(UInt32.Parse(val));
                        }
                        catch { }
                    }
                    break;

                case "EXPIRES":     // Netscape Style Set-Cookie
                    if (cookie.Expires != DateTime.MinValue)
                    {
                        break;
                    }

                    cookie.Expires = TryParseCookieExpires(val);
                    break;

                case "PATH":
                    cookie.Path = val;
                    break;

                case "PORT":
                    if (cookie.Port == null)
                    {
                        cookie.Port = val;
                    }
                    break;

                case "SECURE":
                    cookie.Secure = true;
                    break;

                case "VERSION":
                    try
                    {
                        cookie.Version = (int)UInt32.Parse(val);
                    }
                    catch { }
                    break;
                }
            }

            if (cookie.Domain == "")
            {
                cookie.Domain = host;
            }

            return(cookie);
        }