HttpCookie
參考 :?http://www.cnblogs.com/fish-li/archive/2011/07/03/2096903.html
?
上文結(jié)尾有提到一個(gè)說法
4. HttpRequest.Cookies 與 HttpResponse.Cookies 會(huì)有關(guān)系(很奇怪吧)。
微軟官網(wǎng)也是這么說的 :?https://msdn.microsoft.com/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.
?
這情況有時(shí)候會(huì)讓我們有點(diǎn)混淆。所以我特別寫了一篇來解釋它.
首先我們要知道?
1.
var listx = Response.Cookies.AllKeys.ToList(); //empty list if (Response.Cookies["c"] != null) { } var list = Response.Cookies.AllKeys.ToList(); //["c"] got 1 data即使我們只是讀取,也是再創(chuàng)建?
?
2. httpRequest.cookie 的 expires 永遠(yuǎn)是 0001 年
Response.Cookies["c"].Expires.Year == 1
?
回到真題
所以如果說我們想獲取某些請求的cookie,我們可以使用 httpRequest.cookie['a'] 去拿,這時(shí)你當(dāng)然有可能也會(huì)把response的一起拿出來, 這時(shí)我們需要過濾?
如果cookie.expires.year == 1 就表示他是request 而不是response, 除非你的response 也是year == 1 , ... 這哪里可能=.="?
那么如果我們想要拿一個(gè)response則相反,我們?nèi)ttpResponse拿,然后過濾 year != 1;?
當(dāng)你的request和response 同時(shí)擁有一個(gè)同名的cookie時(shí),你使用 request.cookie 和response.cookie 拿出來的cookie是不同的。
說的有點(diǎn)含糊,但是基本上測試一下就明白了。以后有空才寫整齊版本.
?
轉(zhuǎn)載于:https://www.cnblogs.com/keatkeat/p/4881429.html
總結(jié)
以上是生活随笔為你收集整理的HttpCookie的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Razor语法大全
- 下一篇: OpenSSL(openssl-1.0.