您必须使用反射来查看存储在 cookie 容器中的 Cookie。
使用类似的东西来看看你有什么,然后你可以尝试子类化以获得对你想要的数据的访问,或者通过将 cookie 存储在内存中,从容器中删除它,然后将其添加为一个普通的饼干
public 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 (var 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;
}
public string ShowAllCookies(CookieContainer cc)
{
StringBuilder sb = new StringBuilder();
List<Cookie> lstCookies = GetAllCookies(cc);
sb.AppendLine("=========================================================== ");
sb.AppendLine(lstCookies.Count + " cookies found.");
sb.AppendLine("=========================================================== ");
int cpt = 1;
foreach (Cookie c in lstCookies)
sb.AppendLine("#" + cpt++ + "> Name: " + c.Name + "\tValue: " + c.Value + "\tDomain: " + c.Domain + "\tPath: " + c.Path + "\tExp: " + c.Expires.ToString());
return sb.ToString();
}