2010-05-16 47 views
1

此問題涉及使用How can I get the WebClient to use Cookies?問題中提供的具有cookie功能的WebClient派生類。顯示cookies爲key =所有域的值?

我想使用一個列表框來...

1)顯示每個cookie單獨稱爲「鍵=值」(For Each循環顯示它們全部作爲一個字符串),並

2)能夠顯示所有的cookies,無論從哪個他們來到域( 「www.google.com」,在這裏):

Imports System.IO 
Imports System.Net 

Public Class Form1 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     Dim webClient As New CookieAwareWebClient 
     Const URL = "http://www.google.com" 
     Dim response As String 

     response = webClient.DownloadString(URL) 
     RichTextBox1.Text = response 

     'How to display cookies as key/value in ListBox? 
     'PREF=ID=5e770c1a9f279d5f:TM=1274032511:LM=1274032511:S=1RDPaKJKpoMT9T54 
     For Each mycc In webClient.cc.GetCookies(New Uri(URL)) 
      ListBox1.Items.Add(mycc.ToString) 
     Next 
    End Sub 
End Class 

Public Class CookieAwareWebClient 
    Inherits WebClient 

    Public cc As New CookieContainer() 
    Private lastPage As String 

    Protected Overrides Function GetWebRequest(ByVal address As System.Uri) As System.Net.WebRequest 
     Dim R = MyBase.GetWebRequest(address) 
     If TypeOf R Is HttpWebRequest Then 
      With DirectCast(R, HttpWebRequest) 
       .CookieContainer = cc 
       If Not lastPage Is Nothing Then 
        .Referer = lastPage 
       End If 
      End With 
     End If 
     lastPage = address.ToString() 
     Return R 
    End Function 
End Class 

謝謝。


編輯:下面的代碼,我仍然得到單行鍵=值,而不是單獨的key = value對我需要顯示:

'How to display cookies as key=value in ListBox? 
'still displayed as key=PREF value=ID=c1c024db87787437:TM=1274083167:LM=1274083167:S=ZsG7BXqbCe7yVgJY 
Dim mycookiecollection As CookieCollection 
mycookiecollection = webClient.cc.GetCookies(New Uri(URL)) 
Dim mycookie As Cookie 
For Each mycookie In mycookiecollection 
    ListBox1.Items.Add(mycookie.Name & vbTab & mycookie.Value) 
    'MessageBox.Show(mycookie.Name & vbTab & mycookie.Value) 
Next 

編輯:打開out Google返回了一個key = PREF的cookie,value =多個key = value項的連接。

對於那些有興趣,這裏的一些代碼,通過價值部分解析:

For Each ck As Cookie In cookies 
    Dim ht As New Web.HttpCookie(ck.Name, ck.Value.Replace(":", "&")) 
    If ht.HasKeys Then 
     Debug.WriteLine(ht.Name) 
     For Each key In ht.Values.AllKeys 
      Debug.WriteLine(vbTab & key & vbTab & ht.Values(key)) 
     Next 
    Else 
     Debug.WriteLine(ht.Name & vbTab & ht.Value) 
    End If 
Next 

回答

0

Cookie類有NameValue屬性,你可以使用,而不是調用ToString()只生產適用於使用字符串在HTTP標頭中。

無法枚舉Cookie容器有Cookie的域名,因此您還必須保留域名列表,以便您可以使用GetCookies獲取每個域的Cookie。

+0

感謝進入,但上面的派生類CookieAwareWebClient使用CookieContainer(),它沒有名稱/值屬性。是否有可能將CookieContainer變成Cookie,或者以某種方式從CookieContainer中提取信息? – Gulbahar 2010-05-16 22:57:48

1
CookieContainer cookies = new CookieContainer(); 

// do something to get some cookies 

Hashtable domains= 
    (Hashtable) typeof (CookieContainer) 
    .GetField("m_domainTable", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(cookies); 

foreach (DictionaryEntry domain in domains) 
{ 
    CookieCollection domainCookies = cookies.GetCookies(new Uri("http://" + domain.Key)); 

    foreach (Cookie cookie in domainCookies) 
    { 
     Console.WriteLine("Domain:{0}, Path:{1}, Port:{2}, Name:{3}, Value:{4}", cookie.Domain, cookie.Path, cookie.Port, cookie.Name, cookie.Value); 
    } 

} 
+0

謝謝,但我仍然得到「密鑰= PREF值= ID = c1c024db87787437:TM = 1274083167:LM = 1274083167:S = ZsG7BXqbCe7yVgJY」而不是ID = 123,LM = 12345等。 – Gulbahar 2010-05-17 08:04:31

+1

@Over - 我明白了,代碼是如何從CookieContainer獲取Cookie的示例。不幸的是,與'System.Web.HttpCookie'不同,'System.Net.Cookie'沒有索引器或鍵。您將需要使用正則表達式來解析您的cookie值。如果餅乾形成良好,那麼一個簡單的正則表達式就足夠了。 – 2010-05-17 08:25:52

+0

謝謝。如果沒有更好的方法,我會通過單行cookie來進行regex。 – Gulbahar 2010-05-17 10:24:49

0

CookieContainer對象具有GetCookies方法,它返回一個CookieCollection