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

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

  3. Prasanna Padmanabhan

    ChazZeromus

    Joined:
    Jul 23, 2010
    Messages:
    2
    Likes Received:
    0
    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. Advertisements

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. Ulrich Sprick
    Replies:
    3
    Views:
    603
    Ulrich Sprick
    Oct 7, 2004
  2. Alex Nitulescu

    Response.Cookies vs Request.Cookies

    Alex Nitulescu, Feb 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    9,355
    Hans Kesting
    Feb 3, 2005
  3. Andy Fish
    Replies:
    3
    Views:
    6,856
    Fredrik Lindner
    Nov 6, 2003
  4. user
    Replies:
    3
    Views:
    845
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Mar 31, 2007
  5. archana
    Replies:
    1
    Views:
    643
  6. mhshpk
    Replies:
    0
    Views:
    753
    mhshpk
    Jul 18, 2007
  7. Eric
    Replies:
    1
    Views:
    2,344
    Mark Fitzpatrick
    Dec 28, 2007
  8. _Who
    Replies:
    7
    Views:
    3,134
Loading...