0
 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?

cuongle
  • 74,024
  • 28
  • 151
  • 206
Oktay
  • 127
  • 1
  • 2
  • 15
  • The request isn't timing out, though - the web server is responding. It's responded with HttpStatus of 4XX which indicates an error with the request from the client. The only time your request will timeout is if the server is down, the server doesn't exist, or if the server takes too long to respond to your request. You should therefore check to see if the status code is 200 ("Ok") – dash Nov 05 '12 at 10:57

4 Answers4

1

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!");
 }
dash
  • 89,546
  • 4
  • 51
  • 71
0

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); 
}
Tyress
  • 3,573
  • 2
  • 22
  • 45
  • yes I am throwing webexception however the point is if i don't add any exception to see what is the exception at that moment. it runs without throwing any exception. – Oktay Nov 05 '12 at 10:01
0

i had the same problem before and

catch(WebException e) 
{
 Console.WriteLine(e.toString()); 
}

will solve it

  • Thanks for your answers but webexception doesn't solve problem. As I mentioned when page is down for a while, webexception doesn't work. It works when you tried a page it really doesn't exist. – Oktay Nov 05 '12 at 10:16
  • @Oktay i think now the problem from the server not from your code so what about to handle this issue by set a timeout request and if this time exceed specific period then show a message that the page is down for a while –  Nov 05 '12 at 10:28
  • yes I should do it but i am not sure how to do it. Stopwatch stopWatch2 = new Stopwatch(); stopWatch2.Start(); HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse(); response2.Close(); stopWatch2.Stop(); if i try with stopwatch, I can not set timeout value. do you have a better idea? – Oktay Nov 05 '12 at 13:22
  • You can set this value in web.config. –  Nov 05 '12 at 15:21
0

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());
            }
subZero
  • 307
  • 1
  • 9