[方便查看] CookieContainer转成你看的懂的key-value形式的cookie
CookieContainer 在对象中,你看不到 里面有那些值,
下面这个方法可以讲CookieContainer转成你看的懂的key-value形式的cookie
/// <summary> /// 获取Cookie中存的UserName和UID /// </summary> public static string GetCookieString(System.Net.CookieContainer cc) { string str = ""; List<Cookie> listcookie = GetAllCookies(cc); foreach (Cookie c in listcookie) { string name = c.Name; string value = c.Value; string domain = c.Domain; str += name + "=" + value + ";domain=" + domain + "\n"; } return str; } /// <summary> /// 获取CookieContainer中对象 /// </summary> /// <param name="cc"></param> /// <returns></returns> private static List<Cookie> GetAllCookies(CookieContainer cc) { if (cc == null) { return new List<Cookie>(); } 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; }