Displaying cookies as key=value for all domains?

Posted by OverTheRainbow on Stack Overflow See other posts from Stack Overflow or by OverTheRainbow
Published on 2010-05-16T18:02:07Z Indexed on 2010/05/16 18:30 UTC
Read the original article Hit count: 206

Filed under:
|
|

Hello,

This question pertains to the use of the cookie-capable WebClient derived class presented in the How can I get the WebClient to use Cookies? question.

I'd like to use a ListBox to...

1) display each cookie individually as "key=value" (the For Each loop displays all of them as one string), and

2) be able to display all cookies, regardless of the domain from which they came ("www.google.com", here):

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

Thank you.

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about webclient