473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Response.Cookies vs Request.Cookies

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
1 11404
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Glen | last post by:
Hi, I am looking for a way to delete (expire) all the cookies that we've ever written to the user's machine. I have found several scripts, however I have come across one "gotcha." The scripts...
6
by: Mark | last post by:
Hi... I've come across some weird bug with Response.Cookies. Or maybe it will be called "by design" but for the life of me I can't figure out what purpose it would serve. If you're setting a...
2
by: Reena | last post by:
Hi, Working on .NET 2003. Using C# as server code. I have created a class with following code... using System; using System.Data.OracleClient; using System.Web; using...
1
by: Earl Teigrob | last post by:
When I write inline code on a aspx page using Page.Response.Cookies() to read a cookie, everything works fine, as in the following code. Dim x As Object = Request.Cookies("thing") If x Is Nothing...
14
by: tomer | last post by:
Clear DayHello, I have implemented a download manger in .NET that overrides Internet Explorer's bult-in download manager. I using HttpWebRequest/Response to download the files. All is working...
1
by: Kevin Blount | last post by:
Background: My script is designed to only allow the downloading of a file if a cookie exists to say that someone is logged into my companies site. A colleague set up a test area the extension...
2
by: ad | last post by:
What is different between Request.Cookie and Response.Cookie? Are they independent?
4
by: mike.biang | last post by:
I have an ASP page that is using an XMLHTTP object to request various pages from my server. I keep a single session throughout the XMLHTTP requests by bassing the ASPSESSIONID cookie through the...
10
by: _Who | last post by:
Given Request.Cookies and Response.Cookies in asp.net is there any reason to ever use javascript or any other method to use cookies? Thanks
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.