HttpCookie

编程知识 更新时间:2023-05-01 14:07:57

参考 : http://wwwblogs/fish-li/archive/2011/07/03/2096903.html

 

上文结尾有提到一个说法

4. HttpRequest.Cookies 与 HttpResponse.Cookies 会有关系(很奇怪吧)。

微软官网也是这么说的 : https://msdn.microsoft/en-us/library/system.web.httprequest.cookies(v=vs.110).aspx

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.

 

这情况有时候会让我们有点混淆。所以我特别写了一篇来解释它.

首先我们要知道 

1.

var listx = Response.Cookies.AllKeys.ToList();  //empty list
if (Response.Cookies["c"] != null)
{ 
                                
}
var list = Response.Cookies.AllKeys.ToList(); //["c"] got 1 data 

即使我们只是读取,也是再创建 

 

2. httpRequest.cookie 的 expires 永远是 0001 年

 Response.Cookies["c"].Expires.Year == 1

 

回到真题

所以如果说我们想获取某些请求的cookie,我们可以使用 httpRequest.cookie['a'] 去拿,这时你当然有可能也会把response的一起拿出来, 这时我们需要过滤 

如果cookie.expires.year == 1 就表示他是request 而不是response, 除非你的response 也是year == 1 , ... 这哪里可能=.=" 

那么如果我们想要拿一个response则相反,我们去httpResponse拿,然后过滤 year != 1; 

当你的request和response 同时拥有一个同名的cookie时,你使用 request.cookie 和response.cookie 拿出来的cookie是不同的。

说的有点含糊,但是基本上测试一下就明白了。以后有空才写整齐版本.

 

转载于:https://wwwblogs/keatkeat/p/4881429.html

更多推荐

HttpCookie

本文发布于:2023-04-23 16:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/26ab0f37dec25ee5f02594e02a9f10b5.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:HttpCookie

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 98806文章数
  • 25686阅读数
  • 0评论数