Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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#.

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.

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.

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.

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top