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);