C#cookie合并更新

來源:互聯網
上載者:User

標籤:trim   dex   color   tin   count   sub   類型   重複項   bst   

 

httpcode版本

   /// <summary>        /// 合并Cookie,將cookie2與cookie1合并更新 返回字串類型Cookie        /// </summary>        /// <param name="cookie1">舊cookie</param>        /// <param name="cookie2">新cookie</param>        /// <returns></returns>        public string UpdateCookie(string cookie1, string cookie2)        {            StringBuilder sb = new StringBuilder();            Dictionary<string, string> dicCookie = new Dictionary<string, string>();            //遍曆cookie1            if (!string.IsNullOrEmpty(cookie1))            {                foreach (string cookie in cookie1.Replace(‘,‘, ‘;‘).Split(‘;‘))                {                    if (!string.IsNullOrEmpty(cookie) && cookie.IndexOf(‘=‘) > 0)                    {                        string key = cookie.Split(‘=‘)[0].Trim();                        string value = cookie.Substring(key.Length + 1).Trim();                        if (dicCookie.ContainsKey(key))                        {                            dicCookie[key] = cookie;                        }                        else                        {                            dicCookie.Add(key, cookie);                        }                    }                }            }            if (!string.IsNullOrEmpty(cookie2))            {                //遍曆cookie2                foreach (string cookie in cookie2.Replace(‘,‘, ‘;‘).Split(‘;‘))                {                    if (!string.IsNullOrEmpty(cookie) && cookie.IndexOf(‘=‘) > 0)                    {                        string key = cookie.Split(‘=‘)[0].Trim();                        string value = cookie.Substring(key.Length + 1).Trim();                        if (dicCookie.ContainsKey(key))                        {                            dicCookie[key] = cookie;                        }                        else                        {                            dicCookie.Add(key, cookie);                        }                    }                }            }            int i = 0;            foreach (var item in dicCookie)            {                i++;                if (i < dicCookie.Count)                {                    sb.Append(item.Value + ";");                }                else                {                    sb.Append(item.Value);                }            }            return sb.ToString();        }

萬能架構版本

        /// <summary>        /// 自動合并兩個Cookie的值返回更新後結果         /// </summary>        /// <param name="cookie1">Cookie1</param>        /// <param name="cookie2">Cookie2</param>        /// <returns>返回更新後的Cookie</returns>        internal static string GetMergeCookie(string cookie1, string cookie2)        {            if (string.IsNullOrWhiteSpace(cookie1))//新的是空的            {                return cookie2;//返回老的            }            if (string.IsNullOrWhiteSpace(cookie2))//老的是空的            {                return cookie1;//返回新的            }            List<string> cookielist = new List<string>();//結果            string[] list_1 = cookie1.ToString().Split(‘;‘);            string[] list_2 = cookie2.ToString().Split(‘;‘);            foreach (string item in list_1)            {                //排除重複項                if (cookielist.Contains(item)) continue;                //對接Cookie基本的Key和Value串                cookielist.Add(string.Format("{0} ", item));            }            foreach (string item in list_2)            {                //排除重複項                if (cookielist.Contains(item)) continue;                //對接Cookie基本的Key和Value串                cookielist.Add(string.Format("{0}", item));            }            return string.Join(";", cookielist);        }

 

C#cookie合并更新

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.