CookieContainer不会保存来自Set-Cookie Response Header的所有Cookie [英] CookieContainer doesnt save all cookies that come from Set-Cookie Response Header

查看:106
本文介绍了CookieContainer不会保存来自Set-Cookie Response Header的所有Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,¥b $ b我的问题是,当我使用CookieContainer时,我找不到大多数Cookie,虽然我可以通过Set-Cookie标头获取它们。

这是代码示例

 Dim cookies As New CookieContainer 
Dim req As HttpWebRequest = DirectCast(WebRequest.Create(" https:// twitter。 com"),HttpWebRequest)
req.CookieContainer = cookies
req.Method =" GET"
req.UserAgent =" Mozilla / 5.0(Windows NT 6.1; WOW64; rv:54.0)Gecko / 20100101 Firefox / 54.0"
req.Accept =" text / html,application / xhtml + xml,application / xml; q = 0.9,* / *; q = 0.8"
req.AutomaticDecompression = DecompressionMethods.GZip或DecompressionMethods.Deflate
req.Headers.Add(" DNT"," 1")
req.Headers.Add(" Upgrade-Insecure -Requests"," 1")
使用res As HttpWebResponse = DirectCast(req.GetResponse(),HttpWebResponse)
Console.WriteLine(res.Headers(" Set-Cookie"))'this this IS OUTPUT#1
Console.WriteLine(cookies.GetCookieHeader(req.RequestUri))'这是输出#2
结束使用




输出#1类似于:



 fm = 0; Expires =星期二,2017年8月15日10:24:56 UTC;路径= /;域= .twitter.com;安全;中HTTPOnly,_twitter_sess = BAh7CSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoPY3JlYXRlZF9hdGwrCFLlbOVdAToMY3NyZl9p%250AZCIlM2YzZGJkZWFhYThiM2E4MDk0ZTNiOWE3MGEwNDBiYjI6B2lkIiVjOGU1%250ANzA1NmRjNTQ2MDk1ZTVkZmJhZDUxNGM0MDljOQ%253D 253D% -  01b1bf1e46bdc825a81fc425f47aa0bffe21a8a3;路径= /;域= .twitter.com;安全;中HTTPOnly,personalization_id = QUOT; v1_gDOUQU9tZX / K7 + c8n5Q42Q ==英寸; Expires =星期四,2019年8月15日10:25:05 UTC;路径= /;域= .twitter.com,guest_id = V1%3A150279270536074494; Expires =星期四,2019年8月15日10:25:05 UTC;路径= /;域= .twitter.com,CT0 = d04b5e8f8fab836a8c0120ce08359343; Expires =星期二,2017年8月15日16:25:05 UTC;路径= /;域= .twitter.com;安全




输出#2如下:



 _twitter_sess = BAh7CSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoPY3JlYXRlZF9hdGwrCFLlbOVdAToMY3NyZl9p%250AZCIlM2YzZGJkZWFhYThiM2E4MDk0ZTNiOWE3MGEwNDBiYjI6B2lkIiVjOGU1%250ANzA1NmRjNTQ2MDk1ZTVkZmJhZDUxNGM0MDljOQ%253D 253D% -  01b1bf1e46bdc825a81fc425f47aa0bffe21a8a3 




所以,问题是:

如果我为之前的请求使用了CookieContainer,然后我发送了另一个请求与SAME cookie容器,它只是发送了_twitter_sess(我通过fiddler检查过),但它应该发送我从中得到的所有其他的COOK ;的Set-Cookie"
标题..

解决方案

您好Youssef13,


根据您的描述,你想在输出#1和输出#2之间显示相同的结果吗?


但是我添加了断点,







使用 res
As HttpWebResponse =
DirectCast (req.GetResponse() ,
HttpWebResponse ),
我发现res中有一个计数cookie。所以我不清楚这个。


这是链接,你可以参考:


https: //stackoverflow.com/questions/5518840/not-all-cookies-being-saved-using-cookiecontainer



Best此致,


Cherry




Hello,
My issue is that I don't find most of cookies when I use CookieContainer although I can get them by Set-Cookie header.
Here is a code sample

        Dim cookies As New CookieContainer
        Dim req As HttpWebRequest = DirectCast(WebRequest.Create("https://twitter.com"), HttpWebRequest)
        req.CookieContainer = cookies
        req.Method = "GET"
        req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0"
        req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
        req.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
        req.Headers.Add("DNT", "1")
        req.Headers.Add("Upgrade-Insecure-Requests", "1")
        Using res As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
            Console.WriteLine(res.Headers("Set-Cookie")) 'THIS IS OUTPUT #1
            Console.WriteLine(cookies.GetCookieHeader(req.RequestUri)) 'THIS IS OUTPUT #2
        End Using


The output #1 is like:

fm=0; Expires=Tue, 15 Aug 2017 10:24:56 UTC; Path=/; Domain=.twitter.com; Secure; HTTPOnly,_twitter_sess=BAh7CSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoPY3JlYXRlZF9hdGwrCFLlbOVdAToMY3NyZl9p%250AZCIlM2YzZGJkZWFhYThiM2E4MDk0ZTNiOWE3MGEwNDBiYjI6B2lkIiVjOGU1%250ANzA1NmRjNTQ2MDk1ZTVkZmJhZDUxNGM0MDljOQ%253D%253D--01b1bf1e46bdc825a81fc425f47aa0bffe21a8a3; Path=/; Domain=.twitter.com; Secure; HTTPOnly,personalization_id="v1_gDOUQU9tZX/K7+c8n5Q42Q=="; Expires=Thu, 15 Aug 2019 10:25:05 UTC; Path=/; Domain=.twitter.com,guest_id=v1%3A150279270536074494; Expires=Thu, 15 Aug 2019 10:25:05 UTC; Path=/; Domain=.twitter.com,ct0=d04b5e8f8fab836a8c0120ce08359343; Expires=Tue, 15 Aug 2017 16:25:05 UTC; Path=/; Domain=.twitter.com; Secure


While the output #2 is like:

_twitter_sess=BAh7CSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoPY3JlYXRlZF9hdGwrCFLlbOVdAToMY3NyZl9p%250AZCIlM2YzZGJkZWFhYThiM2E4MDk0ZTNiOWE3MGEwNDBiYjI6B2lkIiVjOGU1%250ANzA1NmRjNTQ2MDk1ZTVkZmJhZDUxNGM0MDljOQ%253D%253D--01b1bf1e46bdc825a81fc425f47aa0bffe21a8a3


So, The problem is:
If I used a CookieContainer for the previous request, and then I sent another request with the SAME cookie container, it just sent the _twitter_sess ( I checked that by fiddler ), but it should send ALL OF THE OTHER COOKIES that I get from "Set-Cookie" Header..

解决方案

Hi Youssef13,

According to your description, you want to display the same result between output #1 and output #2?

But I add breakpoint in ,

Usingres AsHttpWebResponse= DirectCast(req.GetResponse(), HttpWebResponse), and I find there is one count cookie in res. So I am not clear about this.

Here is the link, you can refer to:

https://stackoverflow.com/questions/5518840/not-all-cookies-being-saved-using-cookiecontainer

Best Regards,

Cherry


这篇关于CookieContainer不会保存来自Set-Cookie Response Header的所有Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆