System.Net.Cookie.ToString C# (CSharp) Method

ToString() private method

private ToString ( StringBuilder sb ) : void
sb System.Text.StringBuilder
return void
        internal void ToString(StringBuilder sb)
        {
            int beforeLength = sb.Length;

            // Add the Cookie version if necessary.
            if (Version != 0)
            {
                sb.Append(SpecialAttributeLiteral + VersionAttributeName + EqualsLiteral); // const strings
                if (IsQuotedVersion) sb.Append('"');
                sb.Append(_version.ToString(NumberFormatInfo.InvariantInfo));
                if (IsQuotedVersion) sb.Append('"');
                sb.Append(SeparatorLiteral);
            }

            // Add the Cookie Name=Value pair.
            sb.Append(Name).Append(EqualsLiteral).Append(Value);

            if (!Plain)
            {
                // Add the Path if necessary.
                if (!_pathImplicit && _path.Length > 0)
                {
                    sb.Append(SeparatorLiteral + SpecialAttributeLiteral + PathAttributeName + EqualsLiteral); // const strings
                    sb.Append(_path);
                }

                // Add the Domain if necessary.
                if (!_domainImplicit && _domain.Length > 0)
                {
                    sb.Append(SeparatorLiteral + SpecialAttributeLiteral + DomainAttributeName + EqualsLiteral); // const strings
                    if (IsQuotedDomain) sb.Append('"');
                    sb.Append(_domain);
                    if (IsQuotedDomain) sb.Append('"');
                }
            }

            // Add the Port if necessary.
            if (!_portImplicit)
            {
                sb.Append(SeparatorLiteral + SpecialAttributeLiteral + PortAttributeName); // const strings
                if (_port.Length > 0)
                {
                    sb.Append(EqualsLiteral);
                    sb.Append(_port);
                }
            }

            // Check to see whether the only thing we added was "=", and if so,
            // remove it so that we leave the StringBuilder unchanged in contents.
            int afterLength = sb.Length;
            if (afterLength == (1 + beforeLength) && sb[beforeLength] == '=')
            {
                sb.Length = beforeLength;
            }
        }

Same methods

Cookie::ToString ( ) : string

Usage Example

        /// <summary>Gets the HTTP cookie header that contains the HTTP cookies that represent the <see cref="T:System.Net.Cookie" /> instances that are associated with a specific URI.</summary>
        /// <returns>An HTTP cookie header, with strings representing <see cref="T:System.Net.Cookie" /> instances delimited by semicolons.</returns>
        /// <param name="uri">The URI of the <see cref="T:System.Net.Cookie" /> instances desired. </param>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="uri" /> is null. </exception>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
        /// </PermissionSet>
        public string GetCookieHeader(System.Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }
            CookieCollection cookieCollection = this.GetCookies(uri);

            if (cookieCollection.Count == 0)
            {
                return(string.Empty);
            }
            StringBuilder stringBuilder = new StringBuilder();

            foreach (object obj in cookieCollection)
            {
                Cookie cookie = (Cookie)obj;
                stringBuilder.Append(cookie.ToString(uri));
                stringBuilder.Append("; ");
            }
            if (stringBuilder.Length > 0)
            {
                stringBuilder.Length -= 2;
            }
            return(stringBuilder.ToString());
        }
All Usage Examples Of System.Net.Cookie::ToString