HttpWebResponse.Cookies

Discussion in 'ASP .Net' started by Prasanna Padmanabhan, May 4, 2005.

  1. 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
    Prasanna Padmanabhan, May 4, 2005
    #1
    1. Advertising

  2. Prasanna Padmanabhan

    Brock Allen Guest

    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
    Brock Allen, May 4, 2005
    #2
    1. Advertising

  3. Prasanna Padmanabhan

    ChazZeromus

    Joined:
    Jul 23, 2010
    Messages:
    2
    Thanks, I'd like the know why the heck doesn't MSDN document that you need to initialize CookieContainer?
    ChazZeromus, Jul 23, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Satinderpal Singh

    HttpWebResponse/HttpWebRequest problem...

    Satinderpal Singh, Jun 2, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    3,793
    Satinderpal Singh
    Jun 4, 2004
  2. Karsten Grombach

    Session Cookie and HttpWebResponse

    Karsten Grombach, Jul 23, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    3,652
    Karsten Grombach
    Jul 23, 2003
  3. Sean Chapman

    HttpWebResponse Problems

    Sean Chapman, Oct 25, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    7,498
    Sean Chapman
    Oct 27, 2003
  4. Ulrich Sprick
    Replies:
    3
    Views:
    497
    Ulrich Sprick
    Oct 7, 2004
  5. _Who
    Replies:
    7
    Views:
    2,635
Loading...

Share This Page