System.Net.CookieContainer.CookieCutter C# (CSharp) Method

CookieCutter() private method

private CookieCutter ( Uri uri, string headerName, string setCookieHeader, bool isThrow ) : CookieCollection
uri Uri
headerName string
setCookieHeader string
isThrow bool
return CookieCollection
        internal CookieCollection CookieCutter(Uri uri, string headerName, string setCookieHeader, bool isThrow) {
            GlobalLog.Print("CookieContainer#" + ValidationHelper.HashString(this) + "::CookieCutter() uri:" + uri + " headerName:" + headerName + " setCookieHeader:" + setCookieHeader + " isThrow:" + isThrow);
            CookieCollection cookies = new CookieCollection();
            CookieVariant variant = CookieVariant.Unknown;
            if (headerName == null) {
                variant = CookieVariant.Default;
            }
            else for (int i = 0; i < HeaderInfo.Length; ++i) {
                if ((String.Compare(headerName, HeaderInfo[i].Name, StringComparison.OrdinalIgnoreCase ) == 0)) {
                    variant  = HeaderInfo[i].Variant;
                }
            }
            bool isLocalDomain = IsLocal(uri.Host);
            try {
                CookieParser parser = new CookieParser(setCookieHeader);
                do {
                    Cookie cookie = parser.Get();
                    GlobalLog.Print("CookieContainer#" + ValidationHelper.HashString(this) + "::CookieCutter() CookieParser returned cookie:" + ValidationHelper.ToString(cookie));
                    if (cookie == null) {
                        break;
                    }

                    //Parser marks invalid cookies this way
                    if (ValidationHelper.IsBlankString(cookie.Name)) {
                        if(isThrow) {
                            throw new CookieException(SR.GetString(SR.net_cookie_format));
                        }
                        //Otherwise, ignore (reject) cookie
                        continue;
                    }

                    // this will set the default values from the response URI
                    // AND will check for cookie validity
                    if(!cookie.VerifySetDefaults(variant, uri, isLocalDomain, m_fqdnMyDomain, true, isThrow)) {
                        continue;
                    }
                    // If many same cookies arrive we collapse them into just one, hence setting
                    // parameter isStrict = true below
                    cookies.InternalAdd(cookie, true);

                } while (true);
            }
            catch (Exception e) {
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) {
                    throw;
                }

                if(isThrow) {
                    throw new CookieException(SR.GetString(SR.net_cookie_parse_header, uri.AbsoluteUri), e);
                }
            }
            catch {
                if(isThrow) {
                    throw new CookieException(SR.GetString(SR.net_cookie_parse_header, uri.AbsoluteUri), new Exception(SR.GetString(SR.net_nonClsCompliantException)));
                }
            }

            foreach (Cookie c in cookies) {
                Add(c, isThrow);
            }

            return cookies;
        }

Usage Example

示例#1
0
        void FillCookies()
        {
            if (webHeaders == null)
            {
                return;
            }

            //
            // Don't terminate response reading on bad cookie value
            //
            string           value;
            CookieCollection cookies = null;

            try {
                value = webHeaders.Get("Set-Cookie");
                if (value != null)
                {
                    cookies = cookie_container.CookieCutter(uri, HttpKnownHeaderNames.SetCookie, value, false);
                }
            } catch {
            }

            try {
                value = webHeaders.Get("Set-Cookie2");
                if (value != null)
                {
                    var cookies2 = cookie_container.CookieCutter(uri, HttpKnownHeaderNames.SetCookie2, value, false);

                    if (cookies != null && cookies.Count != 0)
                    {
                        cookies.Add(cookies2);
                    }
                    else
                    {
                        cookies = cookies2;
                    }
                }
            } catch {
            }

            this.cookieCollection = cookies;
        }