Cookie Container doesn't bound with Set-Cookie response header
-
19-03-2021 - |
Question
OK guys so the issue is that I have following code:
HttpWebRequest req;
HttpWebResponse resp;
// go to the site
req = NetLogHttpWebRequestFactory.Create("http://www.facebook.com/");
resp = (HttpWebResponse)req.GetResponse();
So resp.CookieContainer
and resp.Cookies
are empty, but resp.Headers["set-cookie"]
contains corresponding cookies:
datr=oMjsTlmv7Z1aIDOEVnIA11tQ; expires=Mon, 16-Dec-2013 16:51:44 GMT; path=/; domain=.facebook.com; httponly,reg_fb_gate=http%3A%2F%2Fwww.facebook.com%2F; path=/; domain=.facebook.com,reg_fb_ref=http%3A%2F%2Fwww.facebook.com%2F; path=/; domain=.facebook.com
If I'm going wrong with thinking that resp.CookieContainer
should contain appropriate cookies listed above?
Thanks, guys!
UPDATE
I also tried the cookie container bug fix code listed in question CookieContainer bug? but it also doesn't give a valid result bwcause it's should be called before system uses CookieContainer
but it empty at that moment.
La solution
To solve this problem you have to initialize CookieContainer property of request object
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.CookieContainer = new CookieContainer();
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
foreach (Cookie cookie in response.Cookies)
{
Console.WriteLine(cookie.Name + " = " + cookie.Value);
}
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow