Response.cookies vs Request.cookies

C

Cal Who

I'm having a problem that stems from not be able to determin if I should use
Request.Cookies or Response.cookies.

I need to change a cookie in the code then later check it and also want it
to persist on the client.

So it seems to me that I should be using Response.cookie.add(mm)

and later to check use Response.cookie(mm)

But before I add I should remove the old cookie so the the Response.cookie()
will return the new value. Is that corrrect?

This will cause the cookie to be sent to the client for recording?

Do I ever need to check the Request.cookie collection?

Can I add to it or is it readonly?

When are the values copied from the Request to the Response?

Answers to these questions will help a lot.


Thanks
 
G

Gregory A. Beamer

I'm having a problem that stems from not be able to determin if I
should use Request.Cookies or Response.cookies.

Here is the basic web server cycle (very basic)

1. User clicks link
2. Link requests page
3. Server responds back with page

The objects used correspond to what is happening. If you need data from
the client, it is the Request object. If you want to send data to the
client, it is the Reponse object.

In summary


Response.Cookies writes to the reponse stream. It is used when you are
writing or rewriting data.

Request.Cookies is when you are pulling from the client on the server
side.


Peace and Grace,


--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
C

Cal Who

Gregory A. Beamer said:
Here is the basic web server cycle (very basic)

1. User clicks link
2. Link requests page
3. Server responds back with page

The objects used correspond to what is happening. If you need data from
the client, it is the Request object. If you want to send data to the
client, it is the Reponse object.

In summary


Response.Cookies writes to the reponse stream. It is used when you are
writing or rewriting data.

Request.Cookies is when you are pulling from the client on the server
side.


Peace and Grace,


--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************


I don't know if I have it right but now I check Response.cookie first and if
it is not there I check Request.cookie.

But I still wonder: Suppose I do
Response.cookie.add("Something", "one")
Then
Response.cookie.add("Something", "two")
and then read from Response.cookie.

Do I get "one" or "two"

When I get a chance I need to check unless someone passes on that info.

Thanks
 
G

Gregory A. Beamer

I don't know if I have it right but now I check Response.cookie first
and if it is not there I check Request.cookie.

But I still wonder: Suppose I do
Response.cookie.add("Something", "one")
Then
Response.cookie.add("Something", "two")
and then read from Response.cookie.

Do I get "one" or "two"

When I get a chance I need to check unless someone passes on that
info.


Read this blog entry:
http://tinyurl.com/yh4eq3y

It should better explain how the web works and why you use Request at
certain times and Reponse at certain times. It should clarify your
questions completely.

Peace and Grace,


--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
C

Cal Who

Gregory A. Beamer said:
Read this blog entry:
http://tinyurl.com/yh4eq3y

It should better explain how the web works and why you use Request at
certain times and Reponse at certain times. It should clarify your
questions completely.

Peace and Grace,


--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
You show this question on your site:

But before I add I should remove the old cookie so the the Response.cookie()
will return the new value. Is that corrrect?

But I don't think you answered it.


Another way of asking is:
If I have three statements one right after the other - no round trips
between them.

Response.cookie.add("Something", "one")
Response.cookie.add("Something", "two")
Request.cookie("Something")

What do I get one or two?


Thanks and I'm sure that site helped many!
 
G

Gregory A. Beamer

But before I add I should remove the old cookie so the the
Response.cookie() will return the new value. Is that corrrect?

No need. The method is REQUEST >> RESPOND. If you respond a cookie with
the same name, it overwrites, so you don't have to delete and then
replace.

The problem with deleting, is you have to take a delete trip and then a
write trip. Not effective. This is true as the client is somewhere
across the cloud. The browser stores all user coookies in a single file.
If you look in the temporary internet files (IE) or the cookie cache for
other browsers, you generall see something like:

{userName}@{sitename}

That is where they are stored. The server cookie is stored in a
different location, but you can't easily change it and you would not
want to for a persistent mechanism (one that can be used across
sessions).

Apologies if that was not clear enough.


Peace and Grace,



--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
C

Cal Who

I really appreciate you staying with this.
Could you please address tyhe following?
Run three statements one afrter the other:

Response.cookie.add("Something", "one")
Response.cookie.add("Something", "two")
x=Response.cookie("Something")

I used Request last time - I meant Response.

I believe the above returns "one" - not sure.
So a remove before the second add might make sense.
No?


Thanks

What is the
 
G

Gregory A. Beamer

I really appreciate you staying with this.
Could you please address tyhe following?
Run three statements one afrter the other:

Response.cookie.add("Something", "one")
Response.cookie.add("Something", "two")
x=Response.cookie("Something")

I used Request last time - I meant Response.

I believe the above returns "one" - not sure.
So a remove before the second add might make sense.
No?

First, you can't do it like you are stating here, at least not in the newer
versions of ASP.NET.

Second, you are not rewriting the cookie if you use Cookie.Add() as you are
creating two cookies named Something.

Here is some sample code:

PAGE
---------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function confirmButton() {
return confirm('Are you sure everything is correct?');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="CookieLabel" runat="server" Text="Label"></asp:Label><br
/>
<asp:Button ID="submitButton" runat="server" Text="Click Me!"
onclick="submitButton_Click" OnClientClick="confirmButton" />

</form>
</body>
</html>
---------------------------------------------------


Code

---------------------------------------------------
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Response.Cookies["Something"].Value = "one";
Response.Cookies["Something"].Value = "two";

HttpCookie cookie = Response.Cookies["Something"];

CookieLabel.Text = cookie.Value;
}


}

protected void submitButton_Click(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["Something"];
CookieLabel.Text = cookie.Value;
}

}

---------------------------------------------------

In each case, the cookie label reads "two", as the cookie is reset. If you
want cookie add, you have this:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Response.Cookies.Add(new HttpCookie("Something", "one"));
Response.Cookies.Add(new HttpCookie("Something", "two"));

HttpCookie cookie = Response.Cookies["Something"];

CookieLabel.Text = cookie.Value;
}


}

In this case, the answer is one, as you have two cookies named Something.
Want proof?

Change the form to this

<form id="form1" runat="server">
<asp:Label ID="CookieLabel" runat="server" Text="Label"></asp:Label><br />

<asp:Button ID="submitButton" runat="server" Text="Click Me!"
onclick="submitButton_Click" OnClientClick="confirmButton" /><br />
<asp:Label ID="CountLabel" runat="server" Text="Label"></asp:Label>

</form>


And the Page_Load() to this:


protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Response.Cookies.Add(new HttpCookie("Something", "one"));
Response.Cookies.Add(new HttpCookie("Something", "two"));

HttpCookie cookie = Response.Cookies["Something"];

CookieLabel.Text = cookie.Value;

CountLabel.Text = Response.Cookies.Count.ToString();
}


}


Answer == 2.

Now change submit button click to

protected void submitButton_Click(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["Something"];
CookieLabel.Text = cookie.Value;
CountLabel.Text = Request.Cookies.Count.ToString();
}

And one of the cookies disappears. Why? Because the browser reads that you
have two cookies named Something. It stores cookie 1 ("one") and then
stores cookie 2 over cookie 1. The page now reads:

"two"
1

Copy the code here and play with it. Cookies are not really that hard once
you get the idea that the cookie is stored on the client during a RESPONSE
and sent to the server on a REQUEST.

This is why my Page_Load() initially sets the cookie and the button click
retrieves. I MUST complete one trip completely before the cookie is stored
on the client as I have to send something to the client.

Let's change this a bit and see what happens. Make Page_Load read this:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
HttpCookie cookie1 = Request.Cookies["MyCookie"];

if(cookie1 != null)
CookieLabel.Text = cookie1.Value;

CountLabel.Text = Request.Cookies.Count.ToString();

Response.Cookies.Add(new HttpCookie("MyCookie", "one"));
Response.Cookies.Add(new HttpCookie("MyCookie", "two"));

HttpCookie cookie2 = Request.Cookies["MyCookie"];

if(cookie2 != null)
CookieLabel2.Text = cookie2.Value;

CountLabel2.Text = Request.Cookies.Count.ToString();
}
}

What is expected here?

Cookie1 is null
CookieLabel = ""
CountLabel = 0

Cookie2 = "one"
CookieLabel = "one"
CountLabel = 2

Why? There is some magic in that old top hat they found.

1. Microsoft adds the Response cookies to the Request collection so you can
retrieve. This is actually not a good thing, but it protected newbs from
themselves. In reality, the cookie has not been set on the client, so this
is cheating.

Now, let's call the page again. What can we expect?

Cookie1 is null
CookieLabel = "two"
CountLabel = 1

Cookie2 = "two"
CookieLabel = "one"
CountLabel = 3

What just happened here?

1. The first cookie in the Request collection came from the client and has
"two" in it.
2. The second cookie in the Request collection was the Cookie added with
this line:

Response.Cookies.Add(new HttpCookie("MyCookie", "one"));

3. The third cookie in the Request collection was the Cookie added with
this line:

Response.Cookies.Add(new HttpCookie("MyCookie", "two"));

Examine:

Response collection = 2 cookies
Request collection = 3 cookies

Does this make sense now. Let me restate your question.

Why, if I was overwriting the cookie like this:

Response.cookie.add("Something", "one")
Response.cookie.add("Something", "two")
x=Response.cookie("Something")

am I getting "one" instead of "two". Do you see the reason why now?

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top