Request.Cookies和Response.Cookies之间的区别

我在我的代码中多次使用这两个并且不知道区别是什么,如果设置了cookie,它在请求和响应中是否应该完全相同? 请求是最新的,还是回应?

编辑:

好吧,我得到了请求和响应之间的区别,但如果我输入

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

它大部分时间都一样

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

但我想知道使用这两者有什么区别。

正如大家所说, Request.Cookies应该是来自客户端(浏览器)的Cookie,而Response.Cookies是将发送回客户端(浏览器)的cookie。

有一个黑魔法记录良好的*代码,当您向Response添加Cookie时,会将Response Cookie中的值复制到Request.Cookies 。 结果看起来您在RequestResponse都有相同的cookie。 请注意,这些复制的cookie并非来自客户端…因此请注意做出错误的决定。

以下是有关代码的讨论链接: http : //forums.asp.net/t/1279490.aspx 。 特别是,以下列方式添加的cookie将显示在Request.Cookies集合中:

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

*从Response.Cookies复制cookie的行为记录在HttpResponse.Cookies文章中:

使用HttpResponse.Cookies集合添加cookie后,即使响应尚未发送到客户端,cookie也会立即在HttpRequest.Cookies集合中提供。

在Asp.net中使用单词Response将数据从服务器发送到客户端, Request用于从客户端获取数据(以cookie,查询字符串等forms)等。示例:

 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 

正如你所提到的那样

 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. 

请求cookie是从客户端发送到服务器的内容(因此浏览器提供的内容)。 响应cookie是您要在浏览器中放置的Cookie。 从响应对象接受cookie的浏览器的下一个连接将在请求对象中提供cookie。

取决于具体情况。

请求是每个http请求发送到服务器的数据。 响应是服务器客户端请求后的响应。