views:

232

answers:

8

I want to create a small windows application will go automatically every time period to my site and check if its running fine, if it found it down, not working or have an error "Examples: 404, network error, connection to db failed" it will show a message on my screen.

How can i know that there is an error there programmaticly using any .NET language?

+2  A: 

Use HttpWebRequest, and wrap it in a try catch for WebException. The error code in the exception object will give you the code. 404, etc. If it is 500, you could print the message.

Shawn Simon
+2  A: 

Use the System.Net.WebClient object. It's easier to use than HttpWebRequest. It has a "DownloadString" method that will download the contents of a URL into a string. That method may also throw a WebException error if the server returns a 500. For other errors you can parse the string and look for key words.

David
+6  A: 

It's pretty easy to do with a WebClient. It would look something like this:

    WebClient client = new WebClient();
    try
    {
        string response =
            client.DownloadString("http://www.example.com/tester.cgi");

        // We at least got the file back from the server

        // You could optionally look at the contents of the file
        // for additional error indicators      
        if (response.Contains("ERROR: Something"))
        {
            // Handle
        }
    }
    catch (WebException ex)
    {
        // We couldn't get the file.
        // ... handle, depending on the ex
        //
        // For example, by looking at ex.Status:
        switch (ex.Status)
        {
            case WebExceptionStatus.NameResolutionFailure:
                // ...
            break;
            // ...
        }
    }

You could hook that up to a Timer's Tick event or something to periodically make the check.

Daniel LeCheminant
You should also use DownLoadStringAsync(), so that you can set a reasonable timeout period as well.
Joel Coehoorn
+5  A: 

Why bother? You can get a much better solution for cheap from a provider like RedAlert

The nice thing about this is:

1) It tests your site from outside your firewall, so it can detect a wider variety of problems.

2) It is an impartial 3rd party so you can prove uptime if you need to for an SLA.

3) For a small premium you can have the thing try and diagnose the problems.

4) It can page or e-mail you when there is a problem.

5) You don't need to commission a new server.

Geez, I sound like an ad for the guys, but I promise I don't work for them or get a kickback. I have just been happy with the service for our servers.

BTW: I checked pricing and it is about $20 per site/month. So you could probably pay for a year of the service in less time than it will take to build it yourself.

JohnFx
Why bother? You can get the same solution for FREE from a provider like InternetSeer. Heck, even Google Webmaster Tools offers a similar (though not anywhere as sophisticated) service. I fail to see what justifies those $20 per site per month when others charge $0 for the same service.
RegDwight
I took the question "I want to create a small windows application..." to mean that he wanted to learn how to do something, not find a charge/free service that can perform the service...
Dscoduc
@ Dscoduc, yes you are right, what i wanted is to learn how to do it my self, but i thank anyone helped by anything he knows. i learned new things and services from answers here and thats great :)
Amr ElGarhy
I was unaware of the free options. Very useful. I've been using the redalert guys since 1999, I guess I never noticed when the cheaper/free options came out.
JohnFx
+1  A: 

If you do this, create a special page that exercises any special subsystems, like the data base, file IO, etc, and serves up the results in plain text, not html. This will allow you to parse the returned data easier, and will also catch things like DB or IO problems that may not give you a 404 or 500 HTTP error.

Moose
A: 

If you'd prefer to get email/SMS when your sites are down, try the Are My Sites Up web-based solution.

James Burgess
+1  A: 

Try Adventnet's Application Manager (http://www.manageengine.com/products/applications_manager/), it is free for 5 monitors, and provides excellent monitoring capabilities

You could configure the actions that can be done in case of a failure like send email etc.

Dinesh Manne
+2  A: 

Wanting to perform the same functionality I first looked into third party solutions. One particular service that is free and has been fairly accurate is MonitorUs.

If, however, you are wanting to build your own then I would have one recommendation. Consider using a Head request instead of a get request:

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification. w3.org

Here's a link to Peter Bromberg's article that explains how to perform a Head request in C#.

Dscoduc