public static CookieCollection GetCookies(this System.Net.CookieContainer container) { var cookieCollection = new CookieCollection(); var hashTable = container .GetType() .InvokeMember("m_domainTable", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, null, container, Array.Empty <object>()) as Hashtable; if (hashTable == null) { return(null); } foreach (var key in hashTable.Keys) { var tableName = key .ToString(); if (!hashTable.ContainsKey(tableName !)) { continue; } var values = (SortedList)hashTable[tableName] ! .GetType() .InvokeMember("m_list", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, null, hashTable[tableName], Array.Empty <object>()); if (values == null) { return(null); } foreach (var listKey in values.Keys) { var url = $"https://{tableName.TrimStart('.')}{listKey}"; cookieCollection.Add(container.GetCookies(new Uri(url))); } } return(cookieCollection); }
public static 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; }
/// <summary> /// 获取cookie参数 /// </summary> /// <param name="cc">cookie集合</param> /// <returns></returns> public static string GetGtkByCookieSkey(string key, CookieContainer cc) { 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) { if (c.Name.ToLower() == key) { return c.Value; } } } return ""; }
/// <summary> /// 获取cookie参数 /// </summary> /// <param name="cc">cookie集合</param> /// <returns></returns> public static CookieContainer UpdateCookie(CookieContainer cc,string domain) { CookieContainer newcookie = new CookieContainer(); 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) { c.Domain = domain; c.Path = "/"; newcookie.Add(c); } } return newcookie; }
/// <summary> /// Taken from http://stackoverflow.com/a/15991071 /// </summary> /// <param name="cookieJar"></param> /// <returns></returns> public List<Cookie> GetAllCookies(CookieContainer cookieJar) { List<Cookie> cookieCollection = new List<Cookie>(); Hashtable table = (Hashtable)cookieJar.GetType().InvokeMember("m_domainTable", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, null, cookieJar, new object[] { }); foreach (var tableKey in table.Keys) { String str_tableKey = (string)tableKey; if (str_tableKey[0] == '.') { str_tableKey = str_tableKey.Substring(1); } SortedList list = (SortedList)table[tableKey].GetType().InvokeMember("m_list", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, null, table[tableKey], new object[] { }); foreach (var listKey in list.Keys) { String url = "https://" + str_tableKey + (string)listKey; var cookies = cookieJar.GetCookies(new Uri(url)); foreach (Cookie c in cookies) { cookieCollection.Add(c); } } } return cookieCollection; }
public static List<Cookie> getAllCookies(CookieContainer cc) { List<Cookie> lstCookies = new List<Cookie>(); Hashtable table = (Hashtable)cc.GetType().InvokeMember ("m_domainTable", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, null, cc, new object[] { }); //StringBuilder sb = new StringBuilder(); foreach (object pathList in table.Values) { SortedList lstCookieCol = (SortedList)pathList.GetType().InvokeMember ("m_list", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, null, pathList, new object[] { }); foreach (CookieCollection colCookies in lstCookieCol.Values) { foreach (Cookie c in colCookies) { lstCookies.Add(c); //sb.AppendLine(c.Domain + ":" + c.Name + "____" + c.Value + "\r\n"); } } } return lstCookies; }
private static IEnumerable<IDictionary> GetCookieLists(CookieContainer cookieContainer) { Contract.Requires<ArgumentNullException>(cookieContainer != null); Contract.Ensures(Contract.Result<IEnumerable<IDictionary>>() != null); var containerFields = cookieContainer.GetType().GetRuntimeFields(); Contract.Assume(containerFields != null); var domainTableField = containerFields.FirstOrDefault(x => x.Name == "m_domainTable"); Contract.Assume(domainTableField != null); var domainTable = (IDictionary)domainTableField.GetValue(cookieContainer); Contract.Assume(domainTable != null); return domainTable.Values.OfType<object>().Select(domain => { var domainFields = domain.GetType().GetRuntimeFields(); Contract.Assume(domainFields != null); if (domainFields.Any() == false) return null; var listField = domainFields.First(x => x.Name == "m_list"); return (IDictionary)listField.GetValue(domain); }); }
/// <summary> /// 将CookieContainer转换为string类型 /// </summary> /// <param name="cc"></param> /// <returns></returns> public string GetCookieString() { System.Collections.Generic.List <Cookie> lstCookies = new System.Collections.Generic.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[] { }); StringBuilder sb = new StringBuilder(); 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) { sb.Append(c.Name).Append("=").Append(c.Value).Append(";"); } } } return(sb.ToString()); }
private CookieCollection GetAllCookies(CookieContainer cookieJar) { CookieCollection cookieCollection = new CookieCollection(); Hashtable table = (Hashtable)cookieJar.GetType().InvokeMember("m_domainTable", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, null, cookieJar, new object[] { }); foreach (object tableKeyObj in table.Keys) { string tableKey = (string)tableKeyObj; if (tableKey[0] == '.') tableKey = tableKey.Substring(1); SortedList list = (SortedList)table[tableKeyObj].GetType().InvokeMember("m_list", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, null, table[tableKeyObj], new object[] { }); foreach (object listKeyObj in list.Keys) { Uri uri = new Uri(String.Format("https://{0}{1}", tableKey, listKeyObj.ToStringValue())); cookieCollection.Add( cookieJar.GetCookies(uri)); } } return cookieCollection; }
/// <summary> /// 遍历COOKIE返回键值对 /// </summary> /// <param name="cookie"></param> /// <returns></returns> public IList<Cookie> ergodicCookie(CookieContainer cookie) { var listcookie = new List<Cookie>(); if (cookie.Count > 0) { Hashtable table = (Hashtable)cookie.GetType().InvokeMember("m_domainTable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, cookie, 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) { listcookie.Add(c); } } } return listcookie; } else { return null; } }
private void _BugFix_CookieDomain(CookieContainer cookieContainer) { var table = (System.Collections.Hashtable)cookieContainer.GetType().InvokeMember("m_domainTable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, cookieContainer, new object[] { } ); var keys = new System.Collections.ArrayList(table.Keys); foreach (string keyObj in keys) { string key = keyObj; if (key[0] == '.') { string newKey = key.Remove(0, 1); table[newKey] = table[keyObj]; } } }
/// <summary> /// Gathers all avaiable cookie info from a cookie container into a collection /// </summary> /// <param name="container"></param> /// <returns></returns> public static CookieCollection GetAllCookies(CookieContainer container) { var allCookies = new CookieCollection(); var domainTableField = container.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name == "m_domainTable"); if (domainTableField == null) return allCookies; var domains = (IDictionary)domainTableField.GetValue(container); foreach (var cookies in from object val in domains.Values let type = val.GetType().GetRuntimeFields().First(x => x.Name == "m_list") select (IDictionary)type.GetValue(val) into values from CookieCollection cookies in values.Values select cookies) { allCookies.Add(cookies); } return allCookies; }
public static List<Cookie> GetAllCookies(CookieContainer cc) { List<Cookie> list = new List<Cookie>(); Hashtable hashtable = (Hashtable)cc.GetType().InvokeMember("m_domainTable", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField, null, cc, new object[0]); StringBuilder stringBuilder = new StringBuilder(); foreach (object current in hashtable.Values) { SortedList sortedList = (SortedList)current.GetType().InvokeMember("m_list", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField, null, current, new object[0]); foreach (CookieCollection cookieCollection in sortedList.Values) { foreach (Cookie cookie in cookieCollection) { list.Add(cookie); stringBuilder.AppendLine(string.Concat(new string[] { cookie.Domain, ":", cookie.Name, "____", cookie.Value, "\r\n" })); } } } return list; }
public static string GetAllCookiesToString(CookieContainer cc) { var tmpCookie = string.Empty; var table = (Hashtable) cc.GetType() .InvokeMember("m_domainTable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, cc, new object[] { }); tmpCookie = (from object pathList in table.Values select (SortedList) pathList.GetType() .InvokeMember("m_list", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, pathList, new object[] { }) into lstCookieCol from CookieCollection colCookies in lstCookieCol.Values from Cookie c in colCookies select c).Aggregate(tmpCookie, (current, c) => current + (" " + c.ToString() + ";")); if (tmpCookie.Length > 0) tmpCookie = tmpCookie.Substring(0, tmpCookie.Length - 1); return tmpCookie; }
/// <summary> /// 将CookieContainer转换为string类型 /// </summary> /// <param name="cc"></param> /// <returns></returns> public string CookieToString(CookieContainer cc) { System.Collections.Generic.List<Cookie> lstCookies = new System.Collections.Generic.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[] { }); StringBuilder sb = new StringBuilder(); 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) { sb.Append(c.Name).Append("=").Append(c.Value).Append(";"); } } return sb.ToString(); }
/// <summary> /// 打印cookies内容 /// </summary> /// <param name="cc"></param> public string saveCookies(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); } StringBuilder sbc = new StringBuilder(); List<Cookie> cooklist = lstCookies; foreach (Cookie cookie in cooklist) { sbc.AppendFormat("{0};{1};{2};{3};{4};{5}\r\n", cookie.Domain, cookie.Name, cookie.Path, cookie.Port, cookie.Secure.ToString(), cookie.Value); } return sbc.ToString(); }
/// <summary> /// 遍历CookieContainer /// </summary> /// <param name="cookieContainer"></param> /// <returns>List of cookie</returns> public static Dictionary<string, string> GetAllCookies(CookieContainer cookieContainer) { Dictionary<string, string> cookies = new Dictionary<string, string>(); Hashtable table = (Hashtable)cookieContainer.GetType().InvokeMember("m_domainTable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, cookieContainer, new object[] { }); foreach (string pathList in table.Keys) { StringBuilder _cookie = new StringBuilder(); SortedList cookieColList = (SortedList)table[pathList].GetType().InvokeMember("m_list", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, table[pathList], new object[] { }); foreach (CookieCollection colCookies in cookieColList.Values) foreach (Cookie c in colCookies) _cookie.Append(c.Name + "=" + c.Value + ";"); cookies.Add(pathList, _cookie.ToString().TrimEnd(';')); } return cookies; }
private static CookieCollection IterateOverCookies(HttpWebResponse response) { string setCookie = response.Headers.Get("Set-Cookie"); string[] strings = setCookie.Split(';'); string name = strings.FirstOrDefault(s => s.StartsWith("name")); string value = strings.FirstOrDefault(s => s.StartsWith("name")); string path = strings.FirstOrDefault(s => s.ToLower().StartsWith("path=")); IEnumerable<bool> enumerable = strings.Select(s => s.StartsWith("Do")); string domain = strings.FirstOrDefault(s => s.ToLower().StartsWith("domain=")); cookiejar.Add(new Cookie()); //===== //Cookie cookie2 = response.Cookies[0]; //string s = response.Headers.Get("Set-Cookie"); string[] allCookies = response.Headers.AllKeys; //.Get("cookie"); foreach (string allCookie in allCookies) { string s1 = response.Headers.Get(allCookie); } //CookieCollection collection = cookieContainer.GetCookies(new Uri("http://www.futurelearn.com")); //Cookie cookie2 = collection[0]; CookieContainer tempContainer = new CookieContainer(); foreach (Cookie cookie1 in response.Cookies) { tempContainer.Add(cookie1); } CookieCollection cookieCollection = new CookieCollection(); //testcontainer.GetCookies(new Uri("http://www.futurelearn.com")); Hashtable table = (Hashtable)tempContainer.GetType().InvokeMember("m_domainTable", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, null, tempContainer, new object[] { }); foreach (var key in table.Keys) { foreach (Cookie cook in tempContainer.GetCookies(new Uri(string.Format("https://{0}/", key.ToString().Trim('.'))))) { cookieCollection.Add(cook); Console.WriteLine("Name = {0} ; Value = {1} ; Domain = {2}", cook.Name, cook.Value, cook.Domain); } } //===== return cookieCollection; }
/// <summary> /// 遍历CookieContainer /// </summary> /// <param name="cc"></param> /// <returns></returns> public static List<Cookie> GetAllCookies(CookieContainer cc) { var table = (Hashtable)cc.GetType().InvokeMember("m_domainTable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, cc, new object[] { }); return (from object pathList in table.Values select (SortedList)pathList.GetType() .InvokeMember("m_list", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, pathList, new object[] { }) into lstCookieCol from CookieCollection colCookies in lstCookieCol.Values from Cookie c in colCookies select c).ToList(); }
public static CookieCollection GetAllCookies(CookieContainer cookieJar) { if (cookieJar == null) return null; var cookieCollection = new CookieCollection(); var table = (Hashtable)cookieJar.GetType().InvokeMember("m_domainTable", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, null, cookieJar, new object[] { }); foreach (var tableKey in table.Keys) { var str_tableKey = (string)tableKey; if (str_tableKey[0] == '.') { str_tableKey = str_tableKey.Substring(1); } var list = (SortedList)table[tableKey].GetType().InvokeMember("m_list", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, null, table[tableKey], new object[] { }); foreach (var listKey in list.Keys) { var url = "https://" + str_tableKey + (string)listKey; cookieCollection.Add(cookieJar.GetCookies(new Uri(url))); } } return cookieCollection; }