Sending Cookies on a WebRequest object, times out


A

Andres

I am creating a web request (HttpWebRequest) from a web
page in order to retrieve the Html from another page and
embed it in the calling page. When you create a
HttpWebRequest the request is created empty, so I need to
populate its headers in order to send cookies for
authentication purposes.

This is the code fragment:

....
//Create requests
HttpWebRequest req = (HttpWebRequest) WebRequest.Create
(targetUrl);
//Populate some headers explicitly.
req.UserAgent = Context.Request.Headers["User-Agent"];
req.Accept = Context.Request.Headers["Accept"];
req.ContentType = Context.Request.Headers["Content-Type"];

/* If i comment out the next line, the code will work fine,
otherwise, it will generate a timeout error */
req.Headers.Add("Cookie", Context.Request.Headers
["Cookie"]);

//Create the response object from the request.
WebResponse resp = req.GetResponse();
//Create a reader where I can read the html from the
response.
StreamReader rdr = new StreamReader(resp.GetResponseStream
());
//Write somewhere the html. for now in the output window.
System.Diagnostics.Debug.WriteLine(rdr.ReadToEnd());


Any help will be very much appreciated.

Andres.
 
Ad

Advertisements

A

Andres

Sorry. I didnt include the question.

When I send the Cookie header, the request times out, but
when I don't include cookies (either by using the Cookie
header or using the CookieContainer object) the
application retrieves the data very fast.

How can I send cookies using a HttpWebRequest object? Any
help will be much appreciated.



-----Original Message-----
I am creating a web request (HttpWebRequest) from a web
page in order to retrieve the Html from another page and
embed it in the calling page. When you create a
HttpWebRequest the request is created empty, so I need to
populate its headers in order to send cookies for
authentication purposes.

This is the code fragment:

....
//Create requests
HttpWebRequest req = (HttpWebRequest) WebRequest.Create
(targetUrl);
//Populate some headers explicitly.
req.UserAgent = Context.Request.Headers["User-Agent"];
req.Accept = Context.Request.Headers["Accept"];
req.ContentType = Context.Request.Headers["Content-Type"];

/* If i comment out the next line, the code will work fine,
otherwise, it will generate a timeout error */
req.Headers.Add("Cookie", Context.Request.Headers
["Cookie"]);

//Create the response object from the request.
WebResponse resp = req.GetResponse();
//Create a reader where I can read the html from the
response.
StreamReader rdr = new StreamReader(resp.GetResponseStream
());
//Write somewhere the html. for now in the output window.
System.Diagnostics.Debug.WriteLine(rdr.ReadToEnd());


Any help will be very much appreciated.

Andres.
 
Ad

Advertisements

N

Natty Gur

Hi,

First I run your code on my machine and it work like charm.

If you want to call other page in order to embed its HTML in to your
page you can use Server.Execute().

Natty Gur, CTO
Dao2Com Ltd.
34th Elkalay st. Raanana
Israel , 43000
Phone Numbers:
Office: +972-(0)9-7740261
Fax: +972-(0)9-7740261
Mobile: +972-(0)58-888377
 

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

Top