当前位置: 首页>>代码示例>>C#>>正文


C# HttpCookieCollection.AddCookie方法代码示例

本文整理汇总了C#中System.Web.HttpCookieCollection.AddCookie方法的典型用法代码示例。如果您正苦于以下问题:C# HttpCookieCollection.AddCookie方法的具体用法?C# HttpCookieCollection.AddCookie怎么用?C# HttpCookieCollection.AddCookie使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Web.HttpCookieCollection的用法示例。


在下文中一共展示了HttpCookieCollection.AddCookie方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FillInCookiesCollection

        internal void FillInCookiesCollection(HttpCookieCollection cookieCollection, bool includeResponse) {
            if (_wr == null)
                return;

            String s = _wr.GetKnownRequestHeader(HttpWorkerRequest.HeaderCookie);

            // Parse the cookie server variable.
            // Format: c1=k1=v1&k2=v2; c2=...

            int l = (s != null) ? s.Length : 0;
            int i = 0;
            int j;
            char ch;

            HttpCookie lastCookie = null;

            while (i < l) {
                // find next ';' (don't look to ',' as per 91884)
                j = i;
                while (j < l) {
                    ch = s[j];
                    if (ch == ';')
                        break;
                    j++;
                }

                // create cookie form string
                String cookieString = s.Substring(i, j-i).Trim();
                i = j+1; // next cookie start

                if (cookieString.Length == 0)
                    continue;

                HttpCookie cookie = CreateCookieFromString(cookieString);

                // some cookies starting with '$' are really attributes of the last cookie
                if (lastCookie != null) {
                    String name = cookie.Name;

                    // add known attribute to the last cookie (if any)
                    if (name != null && name.Length > 0 && name[0] == '$') {
                        if (StringUtil.EqualsIgnoreCase(name, "$Path"))
                            lastCookie.Path = cookie.Value;
                        else if (StringUtil.EqualsIgnoreCase(name, "$Domain"))
                            lastCookie.Domain = cookie.Value;

                        continue;
                    }
                }

                // regular cookie
                cookieCollection.AddCookie(cookie, true);
                lastCookie = cookie;

                // goto next cookie
            }

            // Append response cookies
            if (includeResponse) {
                // If we have a reference to the response cookies collection, use it directly
                // rather than going through the Response object (which might not be available, e.g.
                // if we have already transitioned to a WebSockets request).
                HttpCookieCollection storedResponseCookies = _storedResponseCookies;
                if (storedResponseCookies == null && !HasTransitionedToWebSocketRequest && Response != null) {
                    storedResponseCookies = Response.GetCookiesNoCreate();
                }

                if (storedResponseCookies != null && storedResponseCookies.Count > 0) {
                    HttpCookie[] responseCookieArray = new HttpCookie[storedResponseCookies.Count];
                    storedResponseCookies.CopyTo(responseCookieArray, 0);
                    for (int iCookie = 0; iCookie < responseCookieArray.Length; iCookie++)
                        cookieCollection.AddCookie(responseCookieArray[iCookie], append: true);
                }

                // release any stored reference to the response cookie collection
                _storedResponseCookies = null;
            }
        }
开发者ID:uQr,项目名称:referencesource,代码行数:78,代码来源:HttpRequest.cs

示例2: FillInCookiesCollection

 internal void FillInCookiesCollection(HttpCookieCollection cookieCollection, bool includeResponse)
 {
     if (this._wr != null)
     {
         string knownRequestHeader = this._wr.GetKnownRequestHeader(0x19);
         int num = (knownRequestHeader != null) ? knownRequestHeader.Length : 0;
         int startIndex = 0;
         HttpCookie cookie = null;
         while (startIndex < num)
         {
             int num3 = startIndex;
             while (num3 < num)
             {
                 char ch = knownRequestHeader[num3];
                 if (ch == ';')
                 {
                     break;
                 }
                 num3++;
             }
             string s = knownRequestHeader.Substring(startIndex, num3 - startIndex).Trim();
             startIndex = num3 + 1;
             if (s.Length != 0)
             {
                 HttpCookie cookie2 = CreateCookieFromString(s);
                 if (cookie != null)
                 {
                     string name = cookie2.Name;
                     if (((name != null) && (name.Length > 0)) && (name[0] == '$'))
                     {
                         if (StringUtil.EqualsIgnoreCase(name, "$Path"))
                         {
                             cookie.Path = cookie2.Value;
                         }
                         else if (StringUtil.EqualsIgnoreCase(name, "$Domain"))
                         {
                             cookie.Domain = cookie2.Value;
                         }
                         continue;
                     }
                 }
                 cookieCollection.AddCookie(cookie2, true);
                 cookie = cookie2;
             }
         }
         if (includeResponse && (this.Response != null))
         {
             HttpCookieCollection cookies = this.Response.Cookies;
             if (cookies.Count > 0)
             {
                 HttpCookie[] dest = new HttpCookie[cookies.Count];
                 cookies.CopyTo(dest, 0);
                 for (int i = 0; i < dest.Length; i++)
                 {
                     cookieCollection.AddCookie(dest[i], true);
                 }
             }
         }
     }
 }
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:60,代码来源:HttpRequest.cs


注:本文中的System.Web.HttpCookieCollection.AddCookie方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。