/// <summary>
/// 把CookieContainer所有的Cookie读出来
/// </summary>
/// <param name="cc"></param>
/// <returns></returns>
private List <Cookie > GetAllCookies(CookieContainer cc)
{
List<Cookie> lstCookies = new List< Cookie>();
Hashtable table = ( Hashtable)cc.GetType().InvokeMember( "m_domainTable",
System.Reflection. BindingFlags.NonPublic | System.Reflection.BindingFlags .GetField |
System.Reflection. BindingFlags.Instance, null , cc, new object [] { });
foreach (object pathList in table.Values)
{
SortedList lstCookieCol = (SortedList)pathList.GetType().InvokeMember("m_list" ,
System.Reflection. BindingFlags.NonPublic | System.Reflection.BindingFlags .GetField
| System.Reflection. BindingFlags.Instance, null , pathList, new object[] { });
foreach ( CookieCollection colCookies in lstCookieCol.Values)
foreach ( Cookie c in colCookies)
lstCookies.Add(c);
}
return lstCookies;
}
///////////////////////////////////////////////////以下是使用样例代码:
private string GetSkey()
{
string skey = string.Empty;
GetCookie();
List<Cookie> listCookie = GetAllCookies(_cookie);
foreach (Cookie c in listCookie)
{
if (c.Name == "skey")
{
skey = c.Value;
break;
}
}
return skey;
}
从CookieContainer中获取所有Cookie
发表评论