2009-02-13 30 views
15

Sau khi tôi nhận được phản hồi từ httpwebrequest, tôi muốn các cookie thu được để tiết kiệm cho mục đích sử dụng chúng trong một httbwebrequest khác. Tuy nhiên, tôi cần phải chèn CookieCollection vào CookieContainer. Làm thế nào để làm điều đó? Đã cố gắng thực hiện:Làm thế nào để chèn CookieCollection vào CookieContainer?

request.Cookiecontainer.add(response.Cookies); 

nhưng điều này liên tục bị lỗi: Tham chiếu đối tượng không được đặt thành thể hiện của đối tượng.

Trả lời

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

Theo Microsoft:

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

Object tài liệu tham khảo không được đặt để một thể hiện của một đối tượng. -> Tôi hỏi vì lỗi này liên tục xuất hiện trên dòng mã này .. – Skuta

+0

Chỉ cần khởi tạo CookieContainer trước khi bạn thêm cookie –

0
dim cookie as new cookiecontainer 

//request codes here 
//response here 

cookie.add(response.cookies) 
Các vấn đề liên quan