Reputation: 127
Uri URL2 = new Uri(@"http://www......com");
HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(URL2);
request2.Timeout = 10000;
HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse();
I am making webrequest with the codes above. When I write a stupid url such as www.oiasjdilasod.com it throws exception; however when an existing page is not available for few hours I cannot get this exception. it doesn't throw any exception and stop running. When this page is not available i tried at internet explorer, it showed page can not be found http 400 bad request. Do you have any suggestions how to catch this exception?
Upvotes: 0
Views: 2275
Reputation: 3653
What exception are you throwing? A regular Exception won't do, it has to be a WebException.
...
catch(WebException e)
{
Console.WriteLine("Error: "+ e.Status);
}
EDIT:
How about a Timeout Exception (Along with the WebException)?
catch (TimeoutException e)
{
Console.WriteLine(e);
}
catch(WebException e)
{
Console.WriteLine("Error: "+ e.Status);
}
Upvotes: 0
Reputation: 307
This Piece of Code Worked for me
try
{
Uri URL2 = new Uri(@"http://www.*****.com");
HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(URL2);
request2.Timeout = 100;
HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse();
}
catch (WebException e)
{
MessageBox.Show(e.Message.ToString());
}
Upvotes: 0
Reputation:
i had the same problem before and
catch(WebException e)
{
Console.WriteLine(e.toString());
}
will solve it
Upvotes: 0
Reputation: 91490
The fact that you are getting a response back from the server means that it's available, it's just not working properly - therefore the request is made and doesn't time out because the server has responded.
It's just not the response you wanted.
Instead, you should check the StatusCode property.
if(response2.StatusCode != HttpStatusCode.OK)
{
throw new Exception("Site is responding incorrectly!");
}
Upvotes: 1