By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,837 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,837 IT Pros & Developers. It's quick & easy.

Response.Cookies vs Request.Cookies

P: n/a
I have the following very simple colde (while learning about cookies and
session state):

Private Sub cmdAddCookie_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdAddCookie.Click

Dim strCookieName As String
Dim objRandom As New Random()

strCookieName = "MyCookie" & objRandom.Next(Integer.MaxValue).ToString
Dim objCookie As New HttpCookie(strCookieName)
objCookie.Values("Name") = txtUsername.Text
objCookie.Values("Pass") = txtPassword.Text
objCookie.Expires = #12/25/2005#
Response.Cookies.Add(objCookie)
End Sub

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender

Dim strCookie As String
Dim strKey As String

lblResults.Text = ""
For Each strCookie In Response.Cookies
lblResults.Text &= "Cookie Name = " &
Response.Cookies(strCookie).Name
If Response.Cookies(strCookie).HasKeys Then
For Each strKey In Response.Cookies(strCookie).Values
lblResults.Text &= "<li>" & "Key = " & strKey & "/Value = "
& Response.Cookies(strCookie).Values(strKey)
Next strKey
End If
Next strCookie
End Sub

When I run my project, I see only
Cookie Name = ASP.NET_SessionId

I enter something in my two textboxes (username and password) and I press
AddCookie

Now, after the POST, I see something like:
Cookie Name = MyCookie1328144184
a.. Key = Name/Value = User 1
a.. Key = Pass/Value = NewPass

I expected to create a new cookie every time I run my project (that's why I
append a random number, to change the cookie's name).

This code does NOT work properly. However, if in Page_PreRender I change
Response.Cookies to Request.Cookies everything works fine.
What is the difference between Response and Request in this respect ? They
both implement the cookies collection.

In MSDN I read the definition of a HTTP response: "Gets the intrinsic
response object for the current request." Yeah, that is *very* helpful !
It's like defining "a cat" as being "a form of life born from a cat, not a
dog". I can only assume that I send a request and the server sends a
response.

PS. BTW, where are these cookies stored ? I searched my HD high & low.... no
luck yet. I have all kind of things in "C:\Documents and
Settings\Administrator\Local Settings\Temporary Internet Files\", but no
"MyCookie".

Thanks a lot,
Alex. Nitulescu
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Alex Nitulescu wrote:
I have the following very simple colde (while learning about cookies and
session state):

Private Sub cmdAddCookie_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdAddCookie.Click

Dim strCookieName As String
Dim objRandom As New Random()

strCookieName = "MyCookie" & objRandom.Next(Integer.MaxValue).ToString
Dim objCookie As New HttpCookie(strCookieName)
objCookie.Values("Name") = txtUsername.Text
objCookie.Values("Pass") = txtPassword.Text
objCookie.Expires = #12/25/2005#
Response.Cookies.Add(objCookie)
End Sub

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender

Dim strCookie As String
Dim strKey As String

lblResults.Text = ""
For Each strCookie In Response.Cookies
lblResults.Text &= "Cookie Name = " &
Response.Cookies(strCookie).Name
If Response.Cookies(strCookie).HasKeys Then
For Each strKey In Response.Cookies(strCookie).Values
lblResults.Text &= "<li>" & "Key = " & strKey & "/Value = "
& Response.Cookies(strCookie).Values(strKey)
Next strKey
End If
Next strCookie
End Sub

When I run my project, I see only
Cookie Name = ASP.NET_SessionId

I enter something in my two textboxes (username and password) and I press
AddCookie

Now, after the POST, I see something like:
Cookie Name = MyCookie1328144184
a.. Key = Name/Value = User 1
a.. Key = Pass/Value = NewPass

I expected to create a new cookie every time I run my project (that's why I
append a random number, to change the cookie's name).

This code does NOT work properly. However, if in Page_PreRender I change
Response.Cookies to Request.Cookies everything works fine.
What is the difference between Response and Request in this respect ? They
both implement the cookies collection.

In MSDN I read the definition of a HTTP response: "Gets the intrinsic
response object for the current request." Yeah, that is *very* helpful !
It's like defining "a cat" as being "a form of life born from a cat, not a
dog". I can only assume that I send a request and the server sends a
response.

PS. BTW, where are these cookies stored ? I searched my HD high & low.... no
luck yet. I have all kind of things in "C:\Documents and
Settings\Administrator\Local Settings\Temporary Internet Files\", but no
"MyCookie".

Thanks a lot,
Alex. Nitulescu


Request.Cookies are the cookies sent along with the "request"
(that is: from browser to server), Response.Cookies are the
cookies get get sent out (from server to browser).

Your cookies (as received by IE, other browsers use different
directories or files) *are* stored in "Temporary Internet Files",
but the filename is usually derived from the servername, *not*
the cookie-name.

--
Hans Kesting
Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.