Home All Groups Group Topic Archive Search About

Problem with WebClient and Cookies

Author
13 Dec 2006 6:15 PM
Bishop
I'm trying to push the site a cookie, then read a page.  Below is an example
that demonstrates the problem I'm having.



This is my ASP.NET page code:

***** CODE *****
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
        Dim cookie As HttpCookie = Request.Cookies("mykey")

        If Not IsNothing(cookie) Then
            Response.Write("Cookie value is: " & cookie.Values("mykey"))
        Else
            Response.Write("Cookie is Nothing")
        End If
    End Sub
***** /CODE *****



This is my VB.NET code:

***** CODE *****
Imports System.IO
Imports System.Net

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        Dim client As WebClient = New WebClient()
        client.Headers.Add(HttpRequestHeader.Cookie, "mykey=myvalue")
        Dim data As Stream =
client.OpenRead("http://localhost/JunkCookieTest/Default.aspx")
        Dim reader As StreamReader = New StreamReader(data)
        data.ReadTimeout = 6000
        txtOutput.Text = reader.ReadToEnd()
        client.Dispose()
    End Sub
***** /CODE *****

It seems that as long as the word "mykey" exists in the
client.Headers.Add..... line I don't recieve "Cookie is Nothing" but I can't
figure out how to assign it a value.  My value is always blank.

AddThis Social Bookmark Button