Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > HttpWebResponse.Cookies

Reply
Thread Tools

HttpWebResponse.Cookies

 
 
Prasanna Padmanabhan
Guest
Posts: n/a
 
      05-04-2005
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


 
Reply With Quote
 
 
 
 
Brock Allen
Guest
Posts: n/a
 
      05-04-2005
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.

-Brock
DevelopMentor
http://staff.develop.com/ballen



> 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




 
Reply With Quote
 
 
 
 
ChazZeromus ChazZeromus is offline
Junior Member
Join Date: Jul 2010
Posts: 2
 
      07-23-2010
Thanks, I'd like the know why the heck doesn't MSDN document that you need to initialize CookieContainer?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments