tags:

views:

3692

answers:

5

I am looking for the best way to test if a website is alive from a C# application.

Background
My application consists of a Winforms UI, a backend WCF service and a website to publish content to the UI and other consumers. To prevent the situation where the UI starts up and fails to work properly because of a missing WCF service or website being down I have added an app startup check to ensure that all everything is alive.

The application is being writen in C#, .Net 3.5, Visual Studio 2008 SP1

Current Solution
Currently I am making a web request to a test page on the website that will inturn test the web site and then display a result.

WebRequest request = WebRequest.Create("http://localhost/myContentSite/test.aspx");
WebResponse response = request.GetResponse();

I am assuming that if there are no exceptions thown during this call then all is well and the UI can start.

Question
Is this the simplest/ right way or is there some other shneaky call that I do no know about in C# or a better way to do it.

A: 

You'll want to check the status code for OK (status 200).

Robert Rouse
+10  A: 
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response == null || response.StatusCode != HttpStatusCode.OK)
Echostorm
Ahhh! the HttpWebResponse was what I was missing! :) Thanks!
FryHard
+2  A: 

Assuming the WCF service and the website live in the same web app, you can use a "Status" WebService that returns the application status. You probably want to do some of the following:

  • Test that the database is up and running (good connection string, service is up, etc...)
  • Test that the website is working (how exactly depends on the website)
  • Test that WCF is working (how exactly depends on your implementation)
  • Added bonus: you can return some versioning info on the service if you need to support different releases in the future.

Then, you create a client on the Win.Forms app for the WebService. If the WS is not responding (i.e. you get some exception on invoke) then the website is down (like a "general error").
If the WS responds, you can parse the result and make sure that everything works, or if something is broken, return more information.

Sklivvz
They are unfortunately not in the same web app, but this is not too much of a problem.I have already created all the tests for the DB and the ORM :) (but thanx for the suggestion)
FryHard
+1  A: 

from the NDiagnostics project on CodePlex...

public override bool WebSiteIsAvailable(string Url)
{
  string Message = string.Empty;
  HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Url);

  // Set the credentials to the current user account
  request.Credentials = System.Net.CredentialCache.DefaultCredentials;
  request.Method = "GET";

  try
  {
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
      // Do nothing; we're only testing to see if we can get the response
    }
  }
  catch (WebException ex)
  {
    Message += ((Message.Length > 0) ? "\n" : "") + ex.Message;
  }

  return (Message.Length == 0);
}
ZombieSheep
Using HEAD verb can be faster.
Lex Li
GetResponse is slow way ??? thanks
Alhambra Eidos
A: 

While using WebResponse please make sure that you close the response stream ie (.close) else it would hang the meachine after certain repeated execution. Eg

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sURL);
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            // your code here
            response.close();
Maxymus