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

ToServerString() private method

private ToServerString ( ) : string
return string
        internal string ToServerString()
        {
            string result = Name + EqualsLiteral + Value;
            if (_comment != null && _comment.Length > 0)
            {
                result += SeparatorLiteral + CommentAttributeName + EqualsLiteral + _comment;
            }
            if (_commentUri != null)
            {
                result += SeparatorLiteral + CommentUrlAttributeName + EqualsLiteral + QuotesLiteral + _commentUri.ToString() + QuotesLiteral;
            }
            if (_discard)
            {
                result += SeparatorLiteral + DiscardAttributeName;
            }
            if (!_domainImplicit && _domain != null && _domain.Length > 0)
            {
                result += SeparatorLiteral + DomainAttributeName + EqualsLiteral + _domain;
            }
            if (Expires != DateTime.MinValue)
            {
                int seconds = (int)(Expires.ToLocalTime() - DateTime.Now).TotalSeconds;
                if (seconds < 0)
                {
                    // This means that the cookie has already expired. Set Max-Age to 0
                    // so that the client will discard the cookie immediately.
                    seconds = 0;
                }
                result += SeparatorLiteral + MaxAgeAttributeName + EqualsLiteral + seconds.ToString(NumberFormatInfo.InvariantInfo);
            }
            if (!_pathImplicit && _path != null && _path.Length > 0)
            {
                result += SeparatorLiteral + PathAttributeName + EqualsLiteral + _path;
            }
            if (!Plain && !_portImplicit && _port != null && _port.Length > 0)
            {
                // QuotesLiteral are included in _port.
                result += SeparatorLiteral + PortAttributeName + EqualsLiteral + _port;
            }
            if (_version > 0)
            {
                result += SeparatorLiteral + VersionAttributeName + EqualsLiteral + _version.ToString(NumberFormatInfo.InvariantInfo);
            }
            return result == EqualsLiteral ? null : result;
        }

Usage Example

 internal void ComputeCookies()
 {
     if (NetEventSource.IsEnabled)
     {
         NetEventSource.Info(this,
                             $"Entering Set-Cookie: {Headers[HttpResponseHeader.SetCookie]}, Set-Cookie2: {Headers[HttpKnownHeaderNames.SetCookie2]}");
     }
     if (_cookies != null)
     {
         // now go through the collection, and concatenate all the cookies in per-variant strings
         string setCookie2 = null;
         string setCookie  = null;
         for (int index = 0; index < _cookies.Count; index++)
         {
             Cookie cookie       = _cookies[index];
             string cookieString = cookie.ToServerString();
             if (cookieString == null || cookieString.Length == 0)
             {
                 continue;
             }
             if (NetEventSource.IsEnabled)
             {
                 NetEventSource.Info(this, $"Now looking at index:{index} cookie: {cookie}");
             }
             if (cookie.IsRfc2965Variant())
             {
                 setCookie2 = setCookie2 == null ? cookieString : setCookie2 + ", " + cookieString;
             }
             else
             {
                 setCookie = setCookie == null ? cookieString : setCookie + ", " + cookieString;
             }
         }
         if (!string.IsNullOrEmpty(setCookie))
         {
             Headers.Set(HttpKnownHeaderNames.SetCookie, setCookie);
             if (string.IsNullOrEmpty(setCookie2))
             {
                 Headers.Remove(HttpKnownHeaderNames.SetCookie2);
             }
         }
         if (!string.IsNullOrEmpty(setCookie2))
         {
             Headers.Set(HttpKnownHeaderNames.SetCookie2, setCookie2);
             if (string.IsNullOrEmpty(setCookie))
             {
                 Headers.Remove(HttpKnownHeaderNames.SetCookie);
             }
         }
     }
     if (NetEventSource.IsEnabled)
     {
         NetEventSource.Info(this,
                             $"Exiting Set-Cookie: {Headers[HttpResponseHeader.SetCookie]} Set-Cookie2: {Headers[HttpKnownHeaderNames.SetCookie2]}");
     }
 }
All Usage Examples Of System.Net.Cookie::ToServerString