对GET/POST请求返回cookie中的键值对进行重新组合

get/post请求返回的cookie中并不是所有的键值对我们都需要,我们只需要提取我们需要的进行重新组合就可以了。

 

如下图是一个GET请求返回的cookie 

 

 

 

我需要提取其中的 uin,skey等相关键值对。

 

以下函数可以完成我们的需求

public string GetCookieByName(List<string> keylist, string cookie)
         {
            string str = "";
            foreach (string key in keylist)
            {
                Regex regex = new Regex(string.Format("{0}=[^;]+", key));
                Match match = regex.Match(cookie);
                if (match.Success)
                {
                    string value = "";
                    if (str.Length == 0)
                        value = match.Value;
                    else
                        value = "; " + match.Value;
                    str = str + value;
                }
            }
            return str;
        }

  

 

 

  

调用方法:

 

                        List<string> keylist = new List<string> { "pt2gguin", "uin", "skey", "superuin", "superkey", "supertoken", "RK", "ptcz" };
                        cookie = GetCookieByName(keylist, cookie);

  

 

posted @ 2016-02-19 19:59  来了啊老弟  阅读(952)  评论(0)    收藏  举报