2011-12-17 79 views
1

OK小夥子,所以問題是,我有下面的代碼:Cookie的集裝箱不設置Cookie響應頭部約束

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"]包含相應的餅乾:

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,那麼錯了?

謝謝,夥計們!


UPDATE

我也試過問題CookieContainer bug?列出的cookie的容器bug修復代碼,但它也是系統採用CookieContainer之前不會給出一個有效的結果bwcause它應該叫,但在它的空那一刻。

回答

2

爲了解決這個問題,你必須初始化請求對象的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); 
    } 
}