通过CookieContainer得到cookie
public static string GetCookie(this CookieContainer cc,string URL)
{
CookieCollection c = cc.GetCookies(new Uri(URL));
string str = "";
for (int i = 0; i < c.Count; i++)
{
if (str == "")
{
str += c[i].Name + "=" + c[i].Value;
}
else
{
str += "&" + c[i].Name + "=" + c[i].Value;
}
}
return str;
}
通过cookie设置cookiecontainer
public static void SetCC(this CookieContainer cc, string cookie,string URL)
{
CookieCollection c = cc.GetCookies(new Uri(URL));
string[] temp = cookie.Split(';');
for (int i = 0; i < temp.Length; i++)
{
for (int j = 0; j < c.Count; j++)
{
if (c[j].Name == temp[i].Substring(0, temp[i].IndexOf("=")))
{
c[j].Value = temp[i].Substring(temp[i].IndexOf("=") + 1);
}
}
}
}