C# Request.Cookies 和 Response.Cookies 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11800254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Request.Cookies and Response.Cookies
提问by Scott Selby
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?
我在我的代码中多次使用这两种方法,但真的不知道有什么区别,如果设置了 cookie,它在请求和响应中不应该完全相同吗?请求是最新的还是响应?
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.
但我想知道使用两者有什么区别。
采纳答案by Alexei Levenkov
As everyone says Request.Cookiesare supposed to be cookies coming from client (browser) and Response.Cookiesare cookies that will be send back to client (browser).
正如每个人所说Request.Cookies的应该是来自客户端(浏览器)Response.Cookies的 cookie,并且是将被发送回客户端(浏览器)的 cookie。
There is black magicwell documented* code that copies values from Responsecookies to Request.Cookieswhen you add cookies to Response. As result it looks like you have same cookies in both Requestand Response. Note that these copied cookies did not come from the client... so beware of making wrong decisions.
还有黑魔法有据可查*代码从副本值Response饼干Request.Cookies当你添加饼干Response。结果看起来您在Request和 中都有相同的 cookie Response。请注意,这些复制的 cookie 不是来自客户端...所以要小心做出错误的决定。
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.Cookiescollection:
这是有关代码讨论的链接:http: //forums.asp.net/t/1279490.aspx。特别是,通过以下方式添加的 cookie 将显示在Request.Cookies集合中:
Response.Cookies.Add(HttpCookie("MyCookie", "MyValue"))
*The behavior of cookies getting copied from Response.Cookiesis documented in the HttpResponse.Cookiesarticle:
*文章中Response.Cookies记录了 cookie 被复制的行为HttpResponse.Cookies:
After you add a cookie by using the
HttpResponse.Cookiescollection, the cookie is immediately available in theHttpRequest.Cookiescollection, even if the response has not been sent to the client.
使用
HttpResponse.Cookies集合添加 cookie 后,该 cookie 将立即在HttpRequest.Cookies集合中可用,即使响应尚未发送到客户端。
回答by Hozuki
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.
请求 cookie 是从客户端发送到服务器的内容(浏览器提供的内容)。响应 cookie 是您要放置在浏览器中的 cookie。接受来自响应对象的 cookie 的浏览器的下一个连接将在请求对象中提供 cookie。
回答by Phillip Schmidt
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 bythe server tothe client.
请求是随每个 http 请求发送到服务器的数据。响应是请求之后响应由所述服务器向所述客户端。
回答by Waqar Janjua
The word Responseis used in Asp.net to send data from the server to the client and the Requestis used to get the data from the client ( in the form of cookies, query string ) etc. Example:
这个词响应在Asp.net使用从服务器发送数据到客户端和请求用于获取来自客户端的数据(Cookie中的形式,查询字符串)等等。例如:
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.

