Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > how to copy the cookies from a HttpRequest to a HttpWebRequest

Reply
Thread Tools

how to copy the cookies from a HttpRequest to a HttpWebRequest

 
 
Andy Fish
Guest
Posts: n/a
 
      08-19-2005
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


 
Reply With Quote
 
 
 
 
Joerg Jooss
Guest
Posts: n/a
 
      08-21-2005
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:news-
 
Reply With Quote
 
 
 
 
Joerg Jooss
Guest
Posts: n/a
 
      08-22-2005
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:news-
 
Reply With Quote
 
Andy Fish
Guest
Posts: n/a
 
      08-23-2005

"Joerg Jooss" <news-> 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:news-



 
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Give Request.Cookies and Response.Cookies is there any reason to use another method to use cookies? _Who ASP .Net 7 09-18-2008 07:49 PM
HttpRequest[] : priority of cookies, form, querystring collections. ken.prat@gmail.com ASP .Net 1 11-10-2005 11:07 PM
Using HttpWebRequest to post form data with cookies Luis Esteban Valencia Muņoz ASP .Net 2 06-13-2005 08:50 PM
System.Net.HttpWebRequest vs. System.Web.HttpRequest Keith Patrick ASP .Net 11 02-22-2005 08:12 PM
Cookies: Are they handled by HttpWebRequest and HttpWebResponse classes? Ulrich Sprick ASP .Net 3 10-07-2004 08:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57