0

I wrote a simple console application to check and see if my site it up by check a link that always work. The problem was it went down the other day and it didnt notify me, it had the error:

system.net.webexception the operation has timed out at system.net.httpwebrequest.getresponse

I set the timeout as 15000 milliseconds so 15 seconds. I look at firebug when it was requesting and it said the request was aborted, I didnt get to see the status code. But I am wondering why am i getting this expection when I set the timeout?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            WebRequest request = WebRequest.Create("http://MYSITE.com/A/P/LIB/22?encoding=UTF-8&b=100");
            request.Timeout = 15000;
            SmtpClient mailserver = null;
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response == null || response.StatusCode != HttpStatusCode.OK)
                {
                    MailMessage contactMsg = new MailMessage();
                    contactMsg.To.Add(new MailAddress("abc123@mysite.com"));
                    contactMsg.From = new MailAddress("donotreply@mysite.com");
                    contactMsg.Subject = "Site is not responding!";
                    contactMsg.IsBodyHtml = true;
                    contactMsg.Body = "<html><body><h1>The Site is not responding</h1> " +
                        "Please check the server. <br /><br />Thank You</body></html>";
                    mailserver = new SmtpClient("smtp.mysite.com", 25);
                    //Setup email message
                    try
                    {
                        mailserver.Send(contactMsg);
                        mailserver.Dispose();
                    }
                    catch (Exception exc)
                    {
                        Console.WriteLine(exc.ToString());
                        mailserver.Dispose();
                    }
                    Console.WriteLine("Site is Down!");
                }
                else
                {
                    Console.WriteLine("Site is Up!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                mailserver.Dispose();
            }
        }
    }
}
ios85
  • 2,104
  • 7
  • 37
  • 55

1 Answers1

1

Your site isn't responding within the 15 seconds, so the WebException exception is thrown. Timeout is designed to throw an exception.

The Timeout property indicates the length of time, in milliseconds, until the request times out and throws a WebException. The Timeout property affects only synchronous requests made with the GetResponse method. To time out asynchronous requests, use the Abort method.

(MSDN)

keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • So I need to handle this exception the same as I have if the status isnt returned as Ok correct? – ios85 Jan 06 '12 at 16:14
  • Yes, the exception is essentially saying that the server wasn't responding within the given limit. The reason why it wasn't responding can vary. You can try and get the http code from the exception that's thrown. See this answer: http://stackoverflow.com/questions/3614034/system-net-webexception-http-codes – keyboardP Jan 06 '12 at 16:28