Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert CookieCollection to CookieContainer?

After I get response from httpwebrequest, I'd like the cookies obtained to save for the purpose of using them in another httbwebrequest. However, I'd need to insert CookieCollection to CookieContainer. How do I do that? Tried to do:

request.Cookiecontainer.add(response.Cookies);

but this keeps getting out of error: Object reference not set to an instance of an object.

like image 378
Skuta Avatar asked Feb 13 '09 15:02

Skuta


2 Answers

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

According to 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.

like image 90
Todd Friedlich Avatar answered Oct 08 '22 06:10

Todd Friedlich


request.CookieContainer.Add(response.Cookies);
like image 23
John Rasch Avatar answered Oct 08 '22 05:10

John Rasch