Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying cookies as key=value for all domains?
    primarykey
    data
    text
    <p>This question pertains to the use of the cookie-capable WebClient derived class presented in the <a href="https://stackoverflow.com/questions/2825377/how-can-i-get-the-webclient-to-use-cookies">How can I get the WebClient to use Cookies?</a> question.</p> <p>I'd like to use a ListBox to...</p> <p>1) display each cookie individually as "key=value" (the For Each loop displays all of them as one string), and</p> <p>2) be able to display all cookies, regardless of the domain from which they came ("www.google.com", here):</p> <pre><code>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 </code></pre> <p>Thank you.</p> <hr> <p>Edit: With the code below, I still get a single-line key=value instead of the individual key=value pairs I need to display:</p> <pre><code>'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 &amp; vbTab &amp; mycookie.Value) 'MessageBox.Show(mycookie.Name &amp; vbTab &amp; mycookie.Value) Next </code></pre> <hr> <p>Edit: Turns out Google returned a single cookie with key=PREF, and value=a concatenation of multiple key=value items.</p> <p>For those interested, here's some code to parse through the value part:</p> <pre><code>For Each ck As Cookie In cookies Dim ht As New Web.HttpCookie(ck.Name, ck.Value.Replace(":", "&amp;")) If ht.HasKeys Then Debug.WriteLine(ht.Name) For Each key In ht.Values.AllKeys Debug.WriteLine(vbTab &amp; key &amp; vbTab &amp; ht.Values(key)) Next Else Debug.WriteLine(ht.Name &amp; vbTab &amp; ht.Value) End If Next </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload