2012-12-02 40 views
10

कृपया नीचे दिए गए कोड देखें:कुकी कुकीटर के अंदर कुकीज़ जानकारी कैसे प्राप्त करें? (उन सभी को, न कि किसी विशिष्ट डोमेन के लिए)

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

HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
int cookieCount = cookieJar.Count; 

मैं cookieJar अंदर कुकीज़ की जानकारी कैसे मिल सकता है? (वे सभी, सिर्फ एक विशिष्ट डोमेन के लिए नहीं।)
और मैं उस से कुकी कैसे जोड़ या निकाल सकता हूं?

उत्तर

13

प्रतिबिंब, निजी क्षेत्र कि CookieContainer वस्तु में सभी डोमेन अहम प्राप्त करने के लिए इस्तेमाल किया जा सकता

प्र मैं कैसे है कि निजी क्षेत्र के नाम मिल गया हो?

उत्तर। परावर्तक का उपयोग करना;

अपनी घोषित किया जाता है के रूप में:

private Hashtable m_domainTable; 

एक बार हम निजी क्षेत्र मिलता है तो हम डोमेन कुंजी मिल जाएगा, तो कुकीज़ हो रही एक सरल यात्रा है।

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Reflection; 
using System.Net; 
using System.Collections; 

namespace ConsoleApplication4 
{ 


    static class Program 
    { 

     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) 
      { 
       foreach (Cookie cookie in cookies.GetCookies(new Uri(string.Format("http://{0}/", key)))) 
       { 
        Console.WriteLine("Name = {0} ; Value = {1} ; Domain = {2}", cookie.Name, cookie.Value, 
             cookie.Domain); 
       } 
      } 


      Console.Read(); 
     } 

    } 

} 
+0

यह विधि http प्रोटोकॉल और https के लिए बनाई गई कुकीज़ को नहीं दिखाया जाएगा। – user1713059

+0

@ user1713059 - यह कहां दिखाता है कि यह केवल HTTP के लिए ऐसा कर रहा है? वैसे भी, यह http + https दोनों के लिए कुकीज़ प्राप्त करेगा। –

+0

कंसोल प्रिंट लूप में 'string.Format ("http: //' है।' GetCookies' विधि को केवल http के साथ उपसर्ग किए गए डोमेन नामों का उपयोग करके बुलाया जाता है। – user1713059

1

आप एक NUnit परीक्षण लिखने के लिए थे, तो यह कुछ इस तरह होगा:

[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); 
      } 
     } 



    } 
+0

कृपया डोमेन पर परिवर्तनों और दूसरे foreach पर ध्यान दें कथन –

5

धन्यवाद अपने जवाब के लिए AppDeveloper के लिए, यहाँ एक विस्तार पद्धति के रूप में एक से थोड़ा संशोधित संस्करण है।

using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Reflection; 
using System.Text; 

public static class CookieContainerExtension 
{ 
    public static List<Cookie> List(this CookieContainer container) 
    { 
     var cookies = new List<Cookie>(); 

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

     foreach (var key in table.Keys) 
     { 

      Uri uri = null; 

      var domain = key as string; 

      if (domain == null) 
       continue; 

      if (domain.StartsWith(".")) 
       domain = domain.Substring(1); 

      var address = string.Format("http://{0}/", domain); 

      if (Uri.TryCreate(address, UriKind.RelativeOrAbsolute, out uri) == false) 
       continue; 

      foreach (Cookie cookie in container.GetCookies(uri)) 
      { 
       cookies.Add(cookie); 
      } 
     } 

     return cookies; 
    } 
} 

सूची प्राप्त करने के सिर्फ CookieContainer पर सूची (फोन):

CookieContainer cookies = new CookieContainer(); 
cookies.Add(new Cookie("name1", "value1", "/", "www.domain1.com")); 
cookies.Add(new Cookie("name2", "value2", "/", "www.domain2.com")); 
List<Cookie> cookieList = cookies.List(); 
+2

आप फोरच लूप के बाहर अपनी विधि में रिटर्न स्टेटमेंट रखना चाहते हैं। – PixelZerg

3

परिमल राज के कोड का उन्नत संस्करण। यह दोनों, http और https कुकीज़ मुद्रित करेगा। इसे अपनी कक्षा में पेस्ट करने के लिए तैयार है।

// Paste this dependencies in your class 
    using System; 
    using System.Net; 
    using System.Linq; 
    using System.Reflection; 
    using System.Collections; 
    using System.Collections.Generic; 

    /// <summary> 
    /// It prints all cookies in a CookieContainer. Only for testing. 
    /// </summary> 
    /// <param name="cookieJar">A cookie container</param> 
    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); 
     } 
    } 
+1

धन्यवाद आपका कोड काम करता है, लेकिन मुझे कभी-कभी UriFormatException त्रुटियां मिलती हैं। इन सुझावों को प्रिंट करने के तरीके के बारे में कोई सुझाव? –

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^