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

Add() public method

public Add ( Cookie cookie ) : void
cookie Cookie
return void
        public void Add(Cookie cookie) {
            if (cookie == null) {
                throw new ArgumentNullException("cookie");
            }

            if (cookie.Domain.Length == 0) {
                throw new ArgumentException(SR.GetString(SR.net_emptystringcall), "cookie.Domain");
            }

            // We don't know cookie verification status -> re-create cookie and verify it
            Cookie new_cookie = new Cookie(cookie.Name, cookie.Value);
            Uri uri;

            new_cookie.Version = cookie.Version;

            // We cannot add an invalid cookie into the container.
            // Trying to prepare Uri for the cookie verification
            string uriStr = (cookie.Secure ? Uri.UriSchemeHttps : Uri.UriSchemeHttp) + Uri.SchemeDelimiter ;

            if (cookie.Domain[0] == '.') {
                uriStr += "0";                      // Uri cctor should eat this, faked host.
                new_cookie.Domain = cookie.Domain;  // Otherwise keep Domain as implicitly set
            }
            uriStr += cookie.Domain;


            // Either keep Port as implici or set it according to original cookie
            if (cookie.PortList != null) {
                new_cookie.Port = cookie.Port;
                uriStr += ":"+ cookie.PortList[0];
            }

            // Path must be present, set to root by default
            new_cookie.Path = cookie.Path.Length == 0 ? "/" : cookie.Path;
            uriStr += cookie.Path;

            if(!Uri.TryCreate(uriStr, UriKind.Absolute, out uri))
                throw new CookieException(SR.GetString(SR.net_cookie_attribute, "Domain", cookie.Domain));

            new_cookie.VerifySetDefaults(CookieVariant.Unknown, uri, IsLocal(uri.Host), m_fqdnMyDomain, true, true);

            Add(new_cookie, true);
        }

Same methods

CookieContainer::Add ( Cookie cookie, bool throwOnError ) : void
CookieContainer::Add ( CookieCollection cookies ) : void
CookieContainer::Add ( Uri uri, Cookie cookie ) : void
CookieContainer::Add ( Uri uri, CookieCollection cookies ) : void

Usage Example

示例#1
2
        private async Task<bool> SendLoginData(string username, string password)
        {
            CookieContainer cookies = await _webManager.PostData(
                Constants.LOGIN_URL, string.Format(
                    "action=login&username={0}&password={1}",
                    username.Replace(" ", "+"),
                    WebUtility.UrlEncode(password)));

            if (cookies.Count < 2)
            {
                return false;
            }

            var fixedCookieContainer = new CookieContainer();

            // TODO: HUGE HACK. For some reason Windows Phone does not use the Domain Key on a cookie, but only the domain when making requests.
            // Windows 8 won't break on it, but Windows Phone will, since the Domain Key and Domain are different on SA.
            // We need to move this code to a more common place.

            foreach (Cookie cookie in cookies.GetCookies(new Uri(Constants.COOKIE_DOMAIN_URL)))
            {
                var fixedCookie = new Cookie(cookie.Name, cookie.Value, "/", ".somethingawful.com");
                fixedCookieContainer.Add(new Uri(Constants.COOKIE_DOMAIN_URL), fixedCookie);
            }

            await _localStorageManager.SaveCookie(Constants.COOKIE_FILE, cookies, new Uri(Constants.COOKIE_DOMAIN_URL));
            return true;
        }
All Usage Examples Of System.Net.CookieContainer::Add