how to copy the cookies from a HttpRequest to a HttpWebRequest

Discussion in 'ASP .Net' started by Andy Fish, Aug 19, 2005.

  1. Andy Fish

    Andy Fish Guest

    Hi,

    I have a web app that I want to make calls back into itself (preserving the
    session cookies etc).

    I am using a System.Net.HttpWebRequest object to make the request, but I am
    having trouble getting the cookies into it.

    In the current (i.e. incoming) request, the cookies are stored as
    System.Web.HttpCookie objects in a System.Web.HttpCookieCollection. However,
    in the new (outgoing) request, they are System.Net.Cookie objects in a
    System.Net.CookieContainer.

    how did we end up with two parallel but incompatible representations of the
    same thing?!?

    Andy
     
    Andy Fish, Aug 19, 2005
    #1
    1. Advertising

  2. Andy Fish

    Joerg Jooss Guest

    Andy Fish wrote:

    > Hi,
    >
    > I have a web app that I want to make calls back into itself
    > (preserving the session cookies etc).
    >
    > I am using a System.Net.HttpWebRequest object to make the request,
    > but I am having trouble getting the cookies into it.
    >
    > In the current (i.e. incoming) request, the cookies are stored as
    > System.Web.HttpCookie objects in a System.Web.HttpCookieCollection.
    > However, in the new (outgoing) request, they are System.Net.Cookie
    > objects in a System.Net.CookieContainer.
    >
    > how did we end up with two parallel but incompatible representations
    > of the same thing?!?


    The CookieContainer mananages cookies for your HTTP session(s), whereas
    the CookieCollection contains really just the cookies in a particular
    HTTP response. Note that CookieContainer is self-managing: If there's
    an instance associated with a HttpWebRequest, it will automatically
    pick up cookies contained in a HttpWebResponse.

    Cheers,
    --
    http://www.joergjooss.de
    mailto:
     
    Joerg Jooss, Aug 21, 2005
    #2
    1. Advertising

  3. Andy Fish

    Joerg Jooss Guest

    Andy Fish wrote:

    [...]
    > well thanks for the explanation. Unfortunately I still have the
    > problem of how to construct a new HttpWebRequest that contains the
    > same cookies as the current HttpRequest.
    >
    > FWIW my solution was to bypass the cookie mechanisms on both sides:
    >
    > newHttpWebRequest.Headers["Cookie"] = Request.Headers["Cookie"];
    >
    > n.b. this will only work if newHttpWebRequest.CookieContainer is null.


    Seems I missed the fact that you're mixing System.Net and System.Web
    stuff here. Yes, doing this is a pain. If you want to work with objects
    here, you have to create System.Net.Cookies from System.Web.HttpCookies.

    Cheers,
    --
    http://www.joergjooss.de
    mailto:
     
    Joerg Jooss, Aug 22, 2005
    #3
  4. Andy Fish

    Andy Fish Guest

    "Joerg Jooss" <> wrote in message
    news:...
    > Andy Fish wrote:
    >
    > [...]
    >> well thanks for the explanation. Unfortunately I still have the
    >> problem of how to construct a new HttpWebRequest that contains the
    >> same cookies as the current HttpRequest.
    >>
    >> FWIW my solution was to bypass the cookie mechanisms on both sides:
    >>
    >> newHttpWebRequest.Headers["Cookie"] = Request.Headers["Cookie"];
    >>
    >> n.b. this will only work if newHttpWebRequest.CookieContainer is null.

    >
    > Seems I missed the fact that you're mixing System.Net and System.Web
    > stuff here. Yes, doing this is a pain. If you want to work with objects
    > here, you have to create System.Net.Cookies from System.Web.HttpCookies.
    >


    Funnily after I got that working, I ran into all sorts of problems with
    trying to "loop back" from within a web page to invoke another web page on
    the same server using the same cookies. Sometimes I would get the "Access
    Forbidden: Too many users are connected" message but mostly IIS would just
    lock up and I would have to do an iisreset to get it back.

    In the end I had to abandon this approach entirely. Maybe it would work on a
    server OS but not on my XP pro

    > Cheers,
    > --
    > http://www.joergjooss.de
    > mailto:
     
    Andy Fish, Aug 23, 2005
    #4
    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. Keith Patrick
    Replies:
    11
    Views:
    14,954
    Scott Allen
    Feb 22, 2005
  2. Replies:
    1
    Views:
    515
    Bruce Barker
    Nov 10, 2005
  3. Alex
    Replies:
    2
    Views:
    1,270
  4. Replies:
    26
    Views:
    2,157
    Roland Pibinger
    Sep 1, 2006
  5. _Who
    Replies:
    7
    Views:
    2,747
Loading...

Share This Page