上次提了一个这样的需求,但是没有响应...
为什么没有人响应一下?大家都用不着这个功能么?还是说有这个功能?
最近自己写了一个,因水平有限,请高手指导.
[C#] 纯文本查看 复制代码 public static string updateCookie(string oldcookie, string newcookie)
{
List<string> oldcookielist = new List<string>();
if (oldcookie.Contains(";"))
oldcookielist = new List<string>(oldcookie.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries));
else
oldcookielist.Add(oldcookie);
List<string> newcookielist = new List<string>();
if (newcookie.Contains(";"))
newcookielist = new List<string>(newcookie.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries));
else
newcookielist.Add(newcookie);
foreach (string cookie in newcookielist)
{
//Console.WriteLine("cookie:" + cookie);
if (!string.IsNullOrWhiteSpace(cookie))
{
if (!string.IsNullOrWhiteSpace(cookie.Split('=')[1])) //判断cookie的value值是为空,不为空时才进行操作
{
bool isFind = false; //判断是否是新值
for (int i = 0; i < oldcookielist.Count; i++)
{
if (cookie.Split('=')[0] == oldcookielist[i].Split('=')[0])
{
//oldcookielist[i].Split('=')[1] = cookie.Split('=')[1];
oldcookielist[i] = cookie;
isFind = true;
break;
}
}
if (!isFind) //如果计算后还是false,则表示newcookie里出现新值了,将新值添加到老cookie里
oldcookielist.Add(cookie);
}
}
}
oldcookie = string.Empty;
for (int i = 0; i < oldcookielist.Count; i++)
oldcookie += oldcookielist[i] + ";";
return oldcookie;
|