None
None

Reputation: 5670

HttpWebResponse throws time out exception

I am trying to authenticate a HttpWebRequest. My code is like this

 string url = "http://mydomain.com";
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
    req.Headers[HttpRequestHeader.Cookie] = string.Format("{0}={1}", cookie.Name, cookie.Value);
    WebResponse res = req.GetResponse();

Everything goes fine till last line executes(WebResponse res = req.GetResponse();).When last line executes code execution goes back to first line(string url = "string url = "http://mydomain.com";";) and start again,this happens so many times that a time out exception happens.

Upvotes: 1

Views: 270

Answers (2)

Viktor S.
Viktor S.

Reputation: 12815

First - not clear why url is defined like this:

string url = "HttpContext.Current.Request.Url.AbsoluteUri";

This is not a valid URL. But if you have

string url = HttpContext.Current.Request.Url.AbsoluteUri;

Than you are producing a request to that same page you are in. So basically, you simply produce a loop. You call your page from a browser, it calls itself using webrequest, and it calls itself again. So in VS debugger will show you like it goes back to the first line.
Try simply change url definition like below:

string url = "http://wwww.google.com";

If you will do that,it should work fine. Basically, as I understand, you are simply using wrong URL which creates something like a loop (or even better to say: long chain of calls)

Upvotes: 2

Davin Tryon
Davin Tryon

Reputation: 67296

Try to take the quotes off the HttpContext line:

string url = HttpContext.Current.Request.Url.AbsoluteUri;

You are asking for the literal URI address "HttpContext.Current.Request.Url.AbsoluteUri" instead of the actual absolute URI "http://domain.com/" that is contained in the variable HttpContext.Current.Request.Url.AbsoluteUri.

Upvotes: 0

Related Questions