how to copy the cookies from a HttpRequest to a HttpWebRequest

A

Andy Fish

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
 
J

Joerg Jooss

Andy said:
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,
 
J

Joerg Jooss

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,
 
A

Andy Fish

Joerg Jooss said:
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
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top