How to get cookies info inside of a CookieContainer? (All Of Them, Not For A Specific Domain)

后端 未结 6 1572
野性不改
野性不改 2020-12-09 09:48

Please see the code below:

CookieContainer cookieJar = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(\"http://www.g         


        
6条回答
  •  忘掉有多难
    2020-12-09 10:30

    If you were to write a nUnit test, it would be something like this:

        [Test]
        public void Test()
        {
    
            CookieContainer cookies = new CookieContainer();
            cookies.Add(new Cookie("name1", "value1", "/", "www.domain1.com"));
            cookies.Add(new Cookie("name2", "value2", "/", "www.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)
            {
                foreach (Cookie cookie in cookies.GetCookies(new Uri(string.Format("http://{0}/", key.ToString().Substring(1,key.ToString().Length - 1)))))
                {
                    Assert.That(cookie != null);
                    //Console.WriteLine("Name = {0} ; Value = {1} ; Domain = {2}", cookie.Name, cookie.Value,
                    //                  cookie.Domain);
                }
            }
    
    
    
        }
    

提交回复
热议问题