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
|