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

后端 未结 6 1570
野性不改
野性不改 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:25

    Improved version of PaRiMal RaJ's code. This method will print both, http and https cookies. Ready to paste it in your class.

        // Paste this dependencies in your class
        using System;
        using System.Net;
        using System.Linq;
        using System.Reflection;
        using System.Collections;
        using System.Collections.Generic;
    
        /// 
        /// It prints all cookies in a CookieContainer. Only for testing.
        /// 
        /// A cookie container
        public void PrintCookies (CookieContainer cookieJar)
        {
            try
            {
                Hashtable table = (Hashtable) cookieJar
                    .GetType().InvokeMember("m_domainTable",
                    BindingFlags.NonPublic |
                    BindingFlags.GetField |
                    BindingFlags.Instance,
                    null,
                    cookieJar,
                    new object[] {});
    
    
                foreach (var key in table.Keys)
                {
                    // Look for http cookies.
                    if (cookieJar.GetCookies(
                        new Uri(string.Format("http://{0}/", key))).Count > 0)
                    {
                        Console.WriteLine(cookieJar.Count+" HTTP COOKIES FOUND:");
                        Console.WriteLine("----------------------------------");
                        foreach (Cookie cookie in cookieJar.GetCookies(
                            new Uri(string.Format("http://{0}/", key))))
                        {
                            Console.WriteLine(
                                "Name = {0} ; Value = {1} ; Domain = {2}", 
                                cookie.Name, cookie.Value,cookie.Domain);
                        }
                    }
    
                    // Look for https cookies
                    if (cookieJar.GetCookies(
                        new Uri(string.Format("https://{0}/", key))).Count > 0)
                    {
                        Console.WriteLine(cookieJar.Count+" HTTPS COOKIES FOUND:");
                        Console.WriteLine("----------------------------------");
                        foreach (Cookie cookie in cookieJar.GetCookies(
                            new Uri(string.Format("https://{0}/", key))))
                        {
                            Console.WriteLine(
                                "Name = {0} ; Value = {1} ; Domain = {2}", 
                                cookie.Name, cookie.Value,cookie.Domain);
                        }
                    }
                }
            }
            catch(Exception e)
            {
                Console.WriteLine (e);
            }
        }
    

提交回复
热议问题