Question

I need to write a simple WinForms apps that can be fired to test if a website is still alive and that that website is able to read from a database.

I am using the whole "(HttpWebResponse)myHttpWebRequest.GetResponse()" thing in c# to test whether the site is alive, but I am at a lose for how to get a test page in my website to write something to the "Response" to indicate that it was able to test it's own connectivity to the database.

Here is the sample code for my Winforms side (ripped from the MSDN):

private void CheckUrl()
{
  try
  {
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.google.com");

    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
    myHttpWebResponse.Close();

    label1.Text = myHttpWebRequest.Address.AbsoluteUri;
  }
  catch (WebException e)
  {
    label1.Text = "This program is expected to throw WebException on successful run." +
                                    "\n\nException Message :" + e.Message;

    if (e.Status == WebExceptionStatus.ProtocolError)
    {
      label1.Text = String.Format("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
      label2.Text =String.Format("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
    }
  }
  catch (Exception e)
  {
    label1.Text = e.Message;
  }
}

I was hoping for some help on the webform side of things to return to the above code.

Thanks for any help that you folks can provide.

  • Richard
Was it helpful?

Solution

What happens when your site fails? Does it return a 500 status code or timeout?

Another way to look at it: does it always do something expected if it succeeds?

You might call a URL in your web app that you know will either return a 200 response code or will have some expected HTML markup in the response if things are working fine.

Have your winform call this URL and examine the Response.status or the text in the output buffer for your expected markup. You should also create a timeout in your httprequest. If the page does not load within the timeout, you will get a web exception and you will know the site is failing.

Also, if you have the budget, there are external monitoring services like gomez.com that can automate this and provide reporting on site availability.

OTHER TIPS

You can create a webservice inside of the project called IAMALIVE and have it return a single char.

On your WinForms area, consume said WebService and if it works, your site is alive.

In the essence of Papuccino's answer: you can actually create web services that are embedded in the C# code-behind of your WebForms pages by marking them with the [WebMethod] attribute. Those will reside within the web application, not just the server.

have your webform page open a database connection and perform something simple/low-impact, e.g.

select SystemTableId from dbo.[SystemTable] where SystemTableId = 1

where SystemTable is a single-row table.

If the page throws an exception for any reason, Response.Write the exception message, otherwise Response.Write("SUCCESS") or similar.

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