Difference between Request.Cookies and Response.Cookies

I use both of these many times in my code and don't really know what the difference is , if a cookie is set shouldn't it be exactly the same in request and response? and is request going to the the most up to date , or response?

EDIT:

ok , I get the difference between a request and a response, but if I type

string a = HttpContext.Current.Request.Cookie["a"].Value;

it is most of the time the same as

string a = HttpContext.Current.Response.Cookie["a"].Value;

but I am wondering what is the difference between using the two.

Answers


As everyone says Request.Cookies are supposed to be cookies coming from client (browser) and Response.Cookies are cookies that will be send back to client (browser).

There is black magic well documented* code that copies values from Response cookies to Request.Cookies when you add cookies to Response. As result it looks like you have same cookies in both Request and Response. Note that these copied cookies did not come from the client... so beware of making wrong decisions.

Here is a link to discussion about the code: http://forums.asp.net/t/1279490.aspx. In particular, cookies added in the following way will show up in the Request.Cookies collection:

Response.Cookies.Add(HttpCookie("MyCookie", "MyValue"))

*The behavior of cookies getting copied from Response.Cookies is documented in the HttpResponse.Cookies article:

After you add a cookie by using the HttpResponse.Cookies collection, the cookie is immediately available in the HttpRequest.Cookies collection, even if the response has not been sent to the client.


The word Response is used in Asp.net to send data from the server to the client and the Request is used to get the data from the client ( in the form of cookies, query string ) etc. Example:

Response.Write("will write the content on the form which will return to the client");
// Response.Cookies will send the cookie to the client browser.
 Response.Cookies.Add(HttpCookie("MyCookie", "MyValue"))
//and Request.Cookies is used to get the cookie value which is already present in the clinet browswer   

and as you mentioned

string a = HttpContext.Current.Request.Cookie["a"].Value;
// I think this will check the cookie which is present in the client browser [ If client has sent the cookie to the server ]

string a = HttpContext.Current.Response.Cookie["a"].Value;
// and this will see the only Response object. If the cookie present in the response object then it will return you otherwise not.

The request cookie is what is send from the client to the server (thus what the browser provides). The response cookie are the cookies that you want to place in the browser. The next connection from the browser that accepted the cookie from the response object will provide the cookie in the request object.


Depends on what context.

Request is the data that gets sent to the server with every http request. Response is the response after the request by the server to the client.


Need Your Help

Hashtable in C++?

c++ performance map hashtable complexity-theory

I usually use C++ stdlib map whenever I need to store some data associated with a specific type of value (a key value - e.g. a string or other object). The stdlib map implementation is based on trees

Call a function without waiting for it

java python function methods

Hi I was wondering if there was a way of calling a function/method (preferably in Python or Java) and continue execution without waiting for it.