Esempio n. 1
0
        private static NameValueCollection CopyCookies(CookieContainer cookies)
        {
            NameValueCollection nameValueCollection = new NameValueCollection(cookies.Count, StringComparer.OrdinalIgnoreCase);
            BindingFlags        invokeAttr          = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField;

            try
            {
                Hashtable hashtable = (Hashtable)cookies.GetType().InvokeMember("m_domainTable", invokeAttr, null, cookies, new object[0]);
                foreach (object obj in hashtable.Values)
                {
                    SortedList sortedList = (SortedList)obj.GetType().InvokeMember("m_list", invokeAttr, null, obj, new object[0]);
                    foreach (object obj2 in sortedList.Values)
                    {
                        CookieCollection cookieCollection = (CookieCollection)obj2;
                        foreach (object obj3 in cookieCollection)
                        {
                            Cookie cookie = (Cookie)obj3;
                            nameValueCollection.Add(cookie.Name, cookie.Value);
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
            return(nameValueCollection);
        }
Esempio n. 2
0
        private 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);
                    }
                }
            }

            StringBuilder sbc = new StringBuilder();

            foreach (Cookie cookie in lstCookies)
            {
                sbc.AppendFormat("Set-Cookie3: {0}={1}; path=\"{2}\"; domain=\"{3}\"; \"{4}\"; version={5}\r\n",
                                 cookie.Name, cookie.Value, cookie.Path, cookie.Domain, cookie.Port,
                                 cookie.Version.ToString());
            }
            //System.Diagnostics.Debug.Write("写入COOKIE文件");

            File.WriteAllText(Path.Combine(Environment.CurrentDirectory, "cookies.txt"), sbc.ToString(), System.Text.Encoding.Default);
            return(lstCookies);
        }
Esempio n. 3
0
        /// <summary>
        /// 得到cookie值
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="cc"></param>
        /// <returns></returns>
        public static string GetCookie(string cookieName, 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 c1 in colCookies)
                    {
                        lstCookies.Add(c1);
                    }
                }
            }
            var model = lstCookies.Find(p => p.Name == cookieName);

            if (model != null)
            {
                return(model.Value);
            }
            return(string.Empty);
        }
Esempio n. 4
0
        public IEnumerable <Cookie> GetAllCookies()
        {
            var k = (Hashtable)_cookieContainer.GetType()
                    .GetField("m_domainTable", BindingFlags.Instance | BindingFlags.NonPublic)
                    ?.GetValue(_cookieContainer);

            if (k == null)
            {
                yield break;
            }
            foreach (DictionaryEntry element in k)
            {
                var l = (SortedList)element.Value.GetType()
                        .GetField("m_list", BindingFlags.Instance | BindingFlags.NonPublic)
                        ?.GetValue(element.Value);

                if (l != null)
                {
                    foreach (var e in l)
                    {
                        var cl = (CookieCollection)((DictionaryEntry)e).Value;
                        foreach (Cookie fc in cl)
                        {
                            yield return(fc);
                        }
                    }
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Gets ALL cookies. (any domain)
        /// </summary>
        /// <returns>List of cookies.</returns>
        private List <Cookie> getAllCookies()
        {
            var cookies = new List <Cookie>();

            var table = (Hashtable)_cookies.GetType().InvokeMember("m_domainTable",
                                                                   BindingFlags.NonPublic |
                                                                   BindingFlags.GetField |
                                                                   BindingFlags.Instance,
                                                                   null,
                                                                   _cookies,
                                                                   null);

            foreach (string key in table.Keys)
            {
                var item  = table[key];
                var items = (ICollection)item.GetType().GetProperty("Values").GetGetMethod().Invoke(item, null);
                foreach (CookieCollection cc in items)
                {
                    foreach (Cookie cookie in cc)
                    {
                        cookies.Add(cookie);
                    }
                }
            }

            return(cookies);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="cc"></param>
        /// <returns></returns>
        public static string CookieContainerToString(CookieContainer cc)
        {
            if (cc == null)
            {
                return("");
            }
            StringBuilder sb         = new StringBuilder();
            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)
                    {
                        //var h = lstCookies.FirstOrDefault(x => x.Name == c.Name);
                        //if (h != null)
                        //    continue;
                        lstCookies.Add(c);
                        sb.Append("NAME:" + c.Name + ";VALUE:" + c.Value + ";DOMAIN:" + c.Domain + "|");
                    }
                }
            }
            return(sb.ToString());
        }
        /// <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);
            }
        }
Esempio n. 8
0
        public static 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 (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;
                    cookieCollection.Add(cookieJar.GetCookies(new Uri(url)));
                }
            }

            return(cookieCollection);
        }
Esempio n. 9
0
        /// <summary>
        ///Gets all cookies in a CookieContainer. Only for testing.
        /// </summary>
        /// <param name="cookieJar">A cookie container</param>
        public static IEnumerable <Cookie> GetAllCookies(CookieContainer cookieJar)
        {
            var table   = (Hashtable)cookieJar.GetType().InvokeMember("m_domainTable", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, null, cookieJar, new object[] { });
            var cookies = new List <Cookie>();

            foreach (var key in table.Keys)
            {
                try
                {
                    // Look for http cookies.
                    if (cookieJar.GetCookies(new Uri($"http://{key}/")).Count > 0)
                    {
                        foreach (Cookie cookie in cookieJar.GetCookies(new Uri($"http://{key}/")))
                        {
                            cookies.Add(cookie);
                        }
                    }

                    // Look for https cookies
                    if (cookieJar.GetCookies(new Uri($"https://{key}/")).Count > 0)
                    {
                        foreach (Cookie cookie in cookieJar.GetCookies(new Uri($"https://{key}/")))
                        {
                            cookies.Add(cookie);
                        }
                    }
                }
                catch (Exception)
                {
                    // ignore exception, not relevant
                }
            }

            return(cookies);
        }
Esempio n. 10
0
        public static Dictionary <string, Dictionary <string, string> > GetAllUrlCookie(this CookieContainer cookieContainer)
        {
            Hashtable table = (Hashtable)cookieContainer.GetType().InvokeMember("m_domainTable",
                                                                                System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField |
                                                                                System.Reflection.BindingFlags.Instance, null, cookieContainer, new object[] { });
            Dictionary <string, Dictionary <string, string> > dictionary = new Dictionary <string, Dictionary <string, string> >();

            foreach (DictionaryEntry cookieList in table)
            {
                var data = new Dictionary <string, string>();
                dictionary.Add(cookieList.Key.ToString(), data);
                SortedList lstCookieCol = (SortedList)cookieList.Value.GetType().InvokeMember("m_list",
                                                                                              System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField
                                                                                              | System.Reflection.BindingFlags.Instance, null, cookieList.Value, new object[] { });
                foreach (CookieCollection colCookies in lstCookieCol.Values)
                {
                    foreach (Cookie c1 in colCookies)
                    {
                        if (data.ContainsKey(c1.Name))
                        {
                            data[c1.Name] = c1.Value;
                        }
                        else
                        {
                            data.Add(c1.Name, c1.Value);
                        }
                    }
                }
            }
            return(dictionary);
        }
Esempio n. 11
0
        private List <CookieResponse> GetAllCookies(CookieContainer container)
        {
            var allCookies       = new List <CookieResponse>();
            var domainTableField = container.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name == "m_domainTable");
            var domains          = (IDictionary)domainTableField.GetValue(container);

            foreach (var val in domains.Values)
            {
                var type   = val.GetType().GetRuntimeFields().First(x => x.Name == "m_list");
                var values = (IDictionary)type.GetValue(val);
                foreach (CookieCollection cookies in values.Values)
                {
                    foreach (Cookie cookie in cookies)
                    {
                        CookieResponse c = new CookieResponse();
                        c.Domain    = cookie.Domain;
                        c.Expires   = cookie.Expires;
                        c.Name      = cookie.Name;
                        c.Path      = cookie.Path;
                        c.TimeStamp = cookie.TimeStamp;
                        c.Value     = cookie.Value;
                        allCookies.Add(c);
                    }
                }
            }
            return(allCookies);
        }
Esempio n. 12
0
        public string 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[] { });
            string cooklist = "";

            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);
                        if (cooklist != "")
                        {
                            cooklist += ";";
                        }
                        cooklist += c.ToString();
                    }
                }
            }

            return(cooklist);
        }
Esempio n. 13
0
        public List <Cookie> GetCookies()
        {
            List <Cookie> cookieCollection = new List <Cookie>();

            try
            {
                Hashtable m_domainTable = (Hashtable)cookieContainer.GetType().InvokeMember("m_domainTable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, cookieContainer, new object[] { });

                //通过反射CookieContainer类进入其内部对私有变量获取其值。m_domainTable为CookieContainer类中的私有字段,类型为Hashtable
                foreach (object pathList in m_domainTable.Values)
                {
                    //pathList为一个SortList类型的派生类
                    SortedList m_list = (SortedList)pathList.GetType().InvokeMember("m_list", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, pathList, new object[] { });
                    foreach (CookieCollection cookies in m_list.Values)
                    {
                        foreach (Cookie cookie in cookies)
                        {
                            if (!cookieCollection.Contains(cookie))
                            {
                                cookieCollection.Add(cookie);
                            }
                        }
                    }
                }
            }
            catch
            {
            }
            return(cookieCollection);
        }
Esempio n. 14
0
        private static string 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);
                    }
                }
            }
            string skey = string.Empty;

            foreach (Cookie c in lstCookies)
            {
                if (c.Name == "skey")
                {
                    skey = c.Value;
                    break;
                }
            }
            return(skey);
        }
Esempio n. 15
0
        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)
                    {
                        c.Expired = false;
                        c.Expires = DateTime.Now.AddDays(100);
                        lstCookies.Add(c);
                    }
                }
            }

            return(lstCookies);
        }
Esempio n. 16
0
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            //sb.AppendFormat("", this.si

            sb.AppendLine("Cookies:");
            Hashtable table = (Hashtable)CookieContainer.GetType().InvokeMember("m_domainTable",
                                                                                BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, null,
                                                                                CookieContainer, new object[] { });

            foreach (string key in table.Keys)
            {
                string procKey = key.Trim();
                procKey = procKey.Trim('.');
                sb.AppendFormat("URI:{1}{0}", Environment.NewLine, procKey);
                try
                {
                    foreach (Cookie cookie in CookieContainer.GetCookies(new Uri(string.Concat("http://", procKey, "/"))))
                    {
                        sb.AppendFormat("{1}:{2} ; Domain = {3}{0}",
                                        Environment.NewLine, cookie.Name, cookie.Value, cookie.Domain);
                    }
                    sb.AppendLine();
                }
                catch (UriFormatException)
                {
                    sb.AppendFormat("Skipping invalid uri:'{1}'...{0}", Environment.NewLine, procKey);
                }
            }
            return(sb.ToString());
        }
Esempio n. 17
0
        public static AsyncCookieContainer Create(CookieContainer cc)
        {
            AsyncCookieContainer acc         = new AsyncCookieContainer();
            List <Cookie>        listCookies = 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)
                    {
                        listCookies.Add(c);
                    }
                }
            }
            foreach (Cookie cookie in listCookies)
            {
                acc.Add(new Cookie()
                {
                    Name = cookie.Name, Value = cookie.Value, Domain = cookie.Domain, Path = cookie.Path
                });
            }
            return(acc);
        }
Esempio n. 18
0
        private static List <Cookie> GetAllCookies(CookieContainer cc)
        {
            List <Cookie> lstCookies = new List <Cookie>();

            if (cc == null)
            {
                //throw new Exception("系统错误,请重新登陆");
            }
            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);
        }
Esempio n. 19
0
        /// <summary>
        /// 获取容器里的Cookie列表
        /// </summary>
        /// <param name="cc"></param>
        /// <returns></returns>
        public static List <Cookie> GetAllCookies(this CookieContainer cc)
        {
            var listCookies = new List <Cookie>();

            if (cc == null)
            {
                return(listCookies);
            }

            var 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)
            {
                var lstCookieCol = (SortedList)pathList.GetType().InvokeMember("m_list",
                                                                               System.Reflection.BindingFlags.NonPublic |
                                                                               System.Reflection.BindingFlags.GetField |
                                                                               System.Reflection.BindingFlags.Instance,
                                                                               null, pathList, new object[] { });

                listCookies.AddRange(from CookieCollection colCookies in lstCookieCol.Values from Cookie c in colCookies select c);
            }

            return(listCookies);
        }
Esempio n. 20
0
        public static List <Cookie> GetAllCookies(this CookieContainer cookieContainer)
        {
            List <Cookie> listCookie = new List <Cookie>();

            Hashtable hashtable = (Hashtable)cookieContainer.GetType().
                                  InvokeMember("m_domainTable", System.Reflection.BindingFlags.NonPublic |
                                               System.Reflection.BindingFlags.GetField |
                                               System.Reflection.BindingFlags.Instance, null, cookieContainer, Array.Empty <object>());

            foreach (object pathList in hashtable.Values)
            {
                SortedList lstCookieCol = (SortedList)pathList.GetType().
                                          InvokeMember("m_list", System.Reflection.BindingFlags.NonPublic
                                                       | System.Reflection.BindingFlags.GetField
                                                       | System.Reflection.BindingFlags.Instance, null, pathList, Array.Empty <object>());
                foreach (CookieCollection colCookies in lstCookieCol.Values)
                {
                    foreach (Cookie cookie in colCookies)
                    {
                        listCookie.Add(cookie);
                    }
                }
            }
            return(listCookie);
        }
Esempio n. 21
0
        private static CookieCollection GetAllCookies(CookieContainer cookieJar, string scheme = "https")
        {
            var cookieCollection = new CookieCollection();

            Hashtable table = (Hashtable)cookieJar.GetType().InvokeMember("m_domainTable",
                                                                          BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance,
                                                                          null, cookieJar, new object[] { });

            foreach (string rawKey in table.Keys)
            {
                // Skip dots in the beginning, the key value is the domain name for the cookies
                var key = rawKey.TrimStart('.');

                // Invoke the private function to get the list of cookies
                var list = (SortedList)table[rawKey].GetType().InvokeMember("m_list",
                                                                            BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance,
                                                                            null,
                                                                            table[key],
                                                                            new object[] { });

                foreach (var uri in list.Keys.Cast <string>()
                         .Select(listkey => new Uri(scheme + "://" + key + listkey)))
                {
                    cookieCollection.Add(cookieJar.GetCookies(uri));
                }
            }

            return(cookieCollection);
        }
Esempio n. 22
0
        public List <Cookie> GetAllCookies()
        {
            CookieContainer cc         = cookieContainer;
            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)
                    {
                        //c.Path = "/";

                        c.Domain = ".taobao.com";
                        lstCookies.Add(c);
                    }
                }
            }

            return(lstCookies);
        }
Esempio n. 23
0
        public string SerializeCookies()
        {
            List <Cookie> listCookies = new List <Cookie>();

            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 (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)
                    {
                        listCookies.Add(c);
                    }
                }
            }

            StringBuilder sb = new StringBuilder();

            foreach (Cookie cookie in listCookies)
            {
                sb.AppendFormat("{0};{1};{2};{3};{4};{5}\r\n", cookie.Domain, cookie.Name, cookie.Path, cookie.Port, cookie.Secure.ToString(), cookie.Value);
            }

            return(sb.ToString());
        }
Esempio n. 24
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="cookies"></param>
        /// <returns></returns>
        public CookieCollection GetCookies(CookieContainer cookies)
        {
            CookieCollection cookieCollection = new CookieCollection();

            Hashtable table = (Hashtable)cookies.GetType().InvokeMember(
                "m_domainTable",
                BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance,
                null,
                cookies,
                new object[] { }
                );

            foreach (var key in table.Keys)
            {
                Uri uri = new Uri(string.Format("http://{0}/", key));

                foreach (Cookie cookie in cookies.GetCookies(uri))
                {
                    Debug.WriteLine("Name = {0} ; Value = {1} ; Domain = {2}",
                                    cookie.Name, cookie.Value, cookie.Domain);

                    cookieCollection.Add(cookie);
                }
            }
            return(cookieCollection);
        }
Esempio n. 25
0
        public static Dictionary <string, List <Cookie> > GetAllCookies(this CookieContainer container)
        {
            var allCookies       = new Dictionary <string, List <Cookie> >();
            var domainTableField = container.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name == "m_domainTable");
            var domains          = (IDictionary)domainTableField.GetValue(container);

            foreach (var val in domains.Values)
            {
                var type   = val.GetType().GetRuntimeFields().First(x => x.Name == "m_list");
                var values = (IDictionary)type.GetValue(val);

                foreach (CookieCollection cookies in values.Values)
                {
                    var domain = cookies.Cast <Cookie>().FirstOrDefault()?.Domain;
                    if (domain == null)
                    {
                        continue;
                    }
                    if (!allCookies.ContainsKey(domain))
                    {
                        allCookies[domain] = new List <Cookie>();
                    }
                    allCookies[domain].AddRange(cookies.Cast <Cookie>());
                }
            }
            return(allCookies);
        }
Esempio n. 26
0
        public IEnumerable <Cookie> GetAllCookies()
        {
            var k = (Hashtable)cookieContainer
                    .GetType().GetField("m_domainTable", BindingFlags.Instance | BindingFlags.NonPublic)
                    .GetValue(cookieContainer);

            foreach (DictionaryEntry element in k)
            {
                var l = (SortedList)element.Value.GetType()
                        .GetField("m_list", BindingFlags.Instance | BindingFlags.NonPublic)
                        .GetValue(element.Value);
                foreach (object e in l)
                {
                    var cl = (CookieCollection)((DictionaryEntry)e).Value;
                    foreach (Cookie fc in cl)
                    {
                        if (fc.Expires.Equals(DateTime.MinValue) && fc.Expires.Kind == DateTimeKind.Unspecified)
                        {
                            fc.Expires = new DateTime(1, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                        }
                        yield return(fc);
                    }
                }
            }
        }
        public static List <Cookie> GetAllCookiesWeb(CookieContainer cc)
        {
            List <Cookie> lstCookies = new List <Cookie>();
            HttpRequest   request    = HttpContext.Current.Request;
            Hashtable     table      = (Hashtable)cc.GetType().InvokeMember("m_domainTable", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance, null, cc, new object[0]);

            foreach (object pathList in table.Values)
            {
                SortedList lstCookieCol = (SortedList)pathList.GetType().InvokeMember("m_list", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance, null, pathList, new object[0]);
                foreach (CookieCollection colCookies in lstCookieCol.Values)
                {
                    foreach (Cookie c in colCookies)
                    {
                        if (c.Domain.ToLower() != request.Url.Host.ToString())
                        {
                            Cookie c_copy = c;
                            lstCookies.Add(c_copy);
                            c_copy.Domain = request.Url.Host.ToString();
                        }
                        lstCookies.Add(c);
                    }
                }
            }
            return(lstCookies);
        }
Esempio n. 28
0
        /// <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);
        }
Esempio n. 29
0
        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);
        }
Esempio n. 30
0
        private static void Main()
        {
            CookieContainer cookies = new CookieContainer();

            cookies.Add(new Cookie("name1", "value1", "/", "domain1.com"));
            cookies.Add(new Cookie("name2", "value2", "/", "domain2.com"));

            Hashtable table = (Hashtable)cookies.GetType().InvokeMember(
                "m_domainTable",
                BindingFlags.NonPublic |
                BindingFlags.GetField |
                BindingFlags.Instance,
                null,
                cookies,
                new object[] {}
                );

            foreach (var key in table.Keys)
            {
                Uri uri = new Uri(string.Format("http://{0}/", key));
                foreach (Cookie cookie in cookies.GetCookies(uri))
                {
                    Console.WriteLine("Name = {0} ; Value = {1} ; Domain = {2}",
                                      cookie.Name, cookie.Value, cookie.Domain);
                }
            }

            Console.Read();
        }