【问题标题】:Displaying cookies as key=value for all domains?将cookie显示为所有域的键=值?
【发布时间】:2010-05-16 18:02:07
【问题描述】:

此问题与How can I get the WebClient to use Cookies? 问题中提供的支持 cookie 的 WebClient 派生类的使用有关。

我想使用 ListBox 来...

1) 将每个 cookie 单独显示为“key=value”(For Each 循环将它们全部显示为一个字符串),并且

2) 能够显示所有 cookie,无论它们来自哪个域(此处为“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 而不是我需要显示的单个 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

编辑:原来 Google 返回了一个带有 key=PREF 和 value=多个 key=value 项的串联的 cookie。

对于那些感兴趣的人,这里有一些通过值部分解析的代码:

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

【问题讨论】:

    标签: vb.net cookies webclient


    【解决方案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);
        }
    
    }
    

    【讨论】:

    • 谢谢,但我仍然得到“key=PREF value=ID=c1c024db87787437:TM=1274083167:LM=1274083167:S=ZsG7BXqbCe7yVgJY”而不是 ID=123、LM=12345 等。
    • @Over - 我明白,代码是如何从 CookieContainer 中获取 cookie 的示例。不幸的是,与System.Web.HttpCookie 不同,System.Net.Cookie 没有索引器或键。您将需要使用正则表达式来解析您的 cookie 值。如果 cookie 格式正确,那么一个简单的正则表达式就足够了。
    • 谢谢。如果没有更好的方法,我将通过单行 cookie 进行正则表达式。
    【解决方案2】:

    Cookie 类具有您可以使用的 NameValue 属性,而不是调用仅生成适合在 HTTP 标头中使用的字符串的 ToString()

    无法枚举 cookie 容器具有 cookie 的域,因此您还必须保留域列表,以便您可以使用 GetCookies 获取每个域的 cookie。

    【讨论】:

    • 感谢 into,但是上面的派生类 CookieAwareWebClient 使用了 CookieContainer(),它没有 Name/Value 属性。是否可以将 CookieContainer 变成 Cookie,或者以某种方式从 CookieContainer 中提取信息?
    【解决方案3】:

    CookieContainer 对象有一个GetCookies 方法,该方法返回一个CookieCollection

    【讨论】:

      猜你喜欢
      • 2014-02-25
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      • 1970-01-01
      • 2020-10-19
      相关资源
      最近更新 更多