PC Review


Reply
Thread Tools Rate Thread

Defining Cookies for HttpWebRequests

 
 
Pat Allan
Guest
Posts: n/a
 
      12th Jan 2004
I've spent half a day on this with no success, so I'm hoping someone
can point out what I'm doing wrong.

I have an ASP.NET Server-side component that I've created, and as part
of its Render function, it requests another webpage (which the user
defines) through an HttpWebRequest object.
This page that is being called requires the cookie objects that are
sent to the page hosting the control. So, as shown in the code below,
I'm looping through the host page's HttpContext object's Cookies (which
are instances of System.Web.HttpCookie) and creating instances of
System.Net.Cookie. These are then added to a
System.Net.CookieCollection, which is tied to the HttpWebRequest object.

Here's the code, I'll explain the problem below it.

HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(sTreePage+"?load=initial");
CookieContainer cookies = new CookieContainer();
for (int i = 0; i < context.Request.Cookies.Count; i++) {
System.Web.HttpCookie hc = context.Request.Cookies[i];
Cookie c = new Cookie(hc.Name, hc.Value);
c.Domain = "localhost";
cookies.Add(c);
}
request.CookieContainer = cookies;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

The code above runs fine, until it gets to the final line, where it
(eventually) times out on the GetResponse().
If I leave out the cookies section (ie. just use the first and last
lines) it works fine. But I need the data the cookies hold on the next
page. And, If other people are using the control, I'd like this layer
to be as transparent as possible.

The main annoyance is that there's two different, incompatible (afaik)
Cookie classes, so I can't just copy them between collections.

I've tried CookieContainer.Add(Uri, Cookie), using the RequestUri from
the HttpWebRequest object - which only works if the cookie domain is
".local" - but its no different. I have to set the domain, else it
throws an exception. And yes, I'm running this all off my local machine.

So how is what I'm doing wrong? Why isn't this working?
Any suggestions or comments are extremely welcome.

Thanks

Pat Allan
========================
Solutions Developer
Managed Business Outcomes
Melbourne, Australia
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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
Using Multiple HttpWebRequests jperezvazquez@gmail.com Microsoft C# .NET 4 28th Oct 2007 07:57 PM
HttpWebRequests and Redirect Gina_Marano Microsoft C# .NET 5 18th May 2007 06:50 PM
httpwebrequests, cookies and sessionID Mike Microsoft Dot NET 6 28th Jan 2006 06:30 PM
Inserting Cookies into HttpWebRequests Pat Allan Microsoft C# .NET 0 12th Jan 2004 12:59 PM
Re: Getting HttpWebRequests Joerg Jooss Microsoft C# .NET 0 12th Jul 2003 12:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:02 PM.