HttpWebResponse.Cookies

  • Thread starter Prasanna Padmanabhan
  • Start date
P

Prasanna Padmanabhan

I am writing a simple HTTP Client in .NET. I make an HTTP Request and
examine the response.

I get an empty CookieCollection when I do HttpWebResponse.Cookies. However
HttpWebResponse.Headers[Set-Cookie] returns the correct list of cookies
(represented as a comma-separated string) in the response.

Can someone please explain the discrepancy?

Thanks,
Prasanna
 
B

Brock Allen

If you're making several requests and would like the cookies to be maintained
across all of those requests you should initialize the HttpWebRequest.CookieContainer
for each of those requests with an instance you have around. So like this:

CookieContainer cookies = new CookieContainer();
HttpWebRequest r = (HttpWebRequest)WebRequest.Create("Page1.aspx");
r.CookieContainer = cookies;
// do r.GetResponse()
r = (HttpWebRequest)WebRequest.Create("Page2.aspx");
r.CookieContainer = cookies;

Also, even if you're just doing a single request, it's possible the page
does a redirect and the HttpWebRequest handles this automatically, but unless
you've given it a CookieContainer it will drop cookies across the redirect.
 
Joined
Jul 23, 2010
Messages
2
Reaction score
0
Thanks, I'd like the know why the heck doesn't MSDN document that you need to initialize CookieContainer?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top