C#cookie合并更新

2021-05-07 21:28

阅读:628

标签:trim   dex   color   tin   count   sub   类型   重复项   bst   

 

httpcode版本

   /// 
        /// 合并Cookie,将cookie2与cookie1合并更新 返回字符串类型Cookie
        /// 
        /// 旧cookie
        /// 新cookie
        /// 
        public string UpdateCookie(string cookie1, string cookie2)
        {
            StringBuilder sb = new StringBuilder();

            Dictionarystring, string> dicCookie = new Dictionarystring, 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();

        }

万能框架版本

        /// 
        /// 自动合并两个Cookie的值返回更新后结果 
        /// 
        /// Cookie1
        /// Cookie2
        /// 返回更新后的Cookie
        internal static string GetMergeCookie(string cookie1, string cookie2)
        {
            if (string.IsNullOrWhiteSpace(cookie1))//新的是空的
            {
                return cookie2;//返回老的
            }
            if (string.IsNullOrWhiteSpace(cookie2))//老的是空的
            {
                return cookie1;//返回新的
            }
            Liststring> cookielist = new Liststring>();//结果
            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合并更新

标签:trim   dex   color   tin   count   sub   类型   重复项   bst   

原文地址:http://www.cnblogs.com/wangyinlon/p/7636740.html


评论


亲,登录后才可以留言!