Response.cookies vs Request.cookies

Discussion in 'ASP .Net' started by Cal Who, Dec 5, 2009.

  1. Cal Who

    Cal Who Guest

    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
    Cal Who, Dec 5, 2009
    #1
    1. Advertising

  2. " Cal Who" <> wrote in
    news:#:

    > 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! |
    *******************************************
    Gregory A. Beamer, Dec 7, 2009
    #2
    1. Advertising

  3. Cal Who

    Cal Who Guest

    "Gregory A. Beamer" <> wrote in message
    news:Xns9CDA74BBB9CE4gbworld@207.46.248.16...
    >" Cal Who" <> wrote in
    > news:#:
    >
    >> 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! |
    > *******************************************



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



    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
    Cal Who, Dec 7, 2009
    #3
  4. " Cal Who" <> wrote in
    news:#:

    > 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! |
    *******************************************
    Gregory A. Beamer, Dec 8, 2009
    #4
  5. Cal Who

    Cal Who Guest

    "Gregory A. Beamer" <> wrote in message
    news:Xns9CDB7995B95D0gbworld@207.46.248.16...
    >" Cal Who" <> wrote in
    > news:#:
    >
    >> 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! |
    > *******************************************

    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!
    Cal Who, Dec 9, 2009
    #5
  6. " Cal Who" <> wrote in
    news::

    > 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! |
    *******************************************
    Gregory A. Beamer, Dec 9, 2009
    #6
  7. Cal Who

    Cal Who Guest

    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
    Cal Who, Dec 9, 2009
    #7
  8. " Cal Who" <> wrote in news:#H#D0UPeKHA.2596
    @TK2MSFTNGP04.phx.gbl:

    > 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! |
    *******************************************
    Gregory A. Beamer, Dec 10, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Nitulescu

    Response.Cookies vs Request.Cookies

    Alex Nitulescu, Feb 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    8,459
    Hans Kesting
    Feb 3, 2005
  2. user
    Replies:
    3
    Views:
    649
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Mar 31, 2007
  3. mhshpk
    Replies:
    0
    Views:
    538
    mhshpk
    Jul 18, 2007
  4. Eric
    Replies:
    1
    Views:
    2,053
    Mark Fitzpatrick
    Dec 28, 2007
  5. _Who
    Replies:
    7
    Views:
    2,637
Loading...

Share This Page