2009-02-13 41 views
15

當我得到httpwebrequest的響應後,我想要獲得的cookies用於保存,以便在另一個httbwebrequest中使用它們。但是,我需要將CookieCollection插入CookieContainer。我怎麼做?試圖做的事情:如何將CookieCollection插入CookieContainer?

request.Cookiecontainer.add(response.Cookies); 

但這樣不斷出錯:對象引用未設置爲對象的實例。

回答

28
request.CookieContainer = new CookieContainer(); 
request.CookieContainer.Add(response.Cookies); 

據微軟稱:

CookieContainer is a null reference (Nothing in Visual Basic) by default. You must assign a CookieContainer object to the property to have cookies returned in the Cookies property of the HttpWebResponse returned by the GetResponse method.

2
request.CookieContainer.Add(response.Cookies); 
+0

對象引用不設置到對象的實例。 - >我問,因爲這個錯誤不斷彈出這個代碼行。 – Skuta 2009-02-13 15:53:52

0
dim cookie as new cookiecontainer 

//request codes here 
//response here 

cookie.add(response.cookies)