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 re
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.
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.
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.
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 theHttpRequest.Cookies
collection, even if the response has not been sent to the client.