문제

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.

도움이 되었습니까?

해결책

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);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top