Cookie容器没有与Set-Cookie响应头绑定

本文关键字:Set-Cookie 响应 绑定 Cookie | 更新日期: 2023-09-27 18:18:04

好了,伙计们,问题是我有以下代码:

HttpWebRequest req;
HttpWebResponse resp;
// go to the site
req = NetLogHttpWebRequestFactory.Create("http://www.facebook.com/");            
resp = (HttpWebResponse)req.GetResponse();  

因此resp.CookieContainerresp.Cookies为空,但resp.Headers["set-cookie"]包含相应的cookie:

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

如果我认为resp.CookieContainer应该包含上面列出的适当的饼干是错误的?

谢谢,伙计们!


我还尝试了cookie容器错误修复代码中列出的问题CookieContainer错误?但它也没有给出一个有效的结果,因为它应该在系统使用CookieContainer之前调用,但当时它是空的。

Cookie容器没有与Set-Cookie响应头绑定

要解决这个问题,您必须初始化请求对象的CookieContainer属性

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);
    }
}