18

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.

Skuta
  • 5,830
  • 27
  • 60
  • 68

3 Answers3

33
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.

Todd Friedlich
  • 662
  • 1
  • 5
  • 13
2
request.CookieContainer.Add(response.Cookies);
John Rasch
  • 62,489
  • 19
  • 106
  • 139
0
dim cookie as new cookiecontainer

//request codes here
//response here

cookie.add(response.cookies)
Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39