ASP.NET Tip: Testing Web Sites with HttpWebRequest

As a third type of verification, I check certain Web sites to make sure that the server is up, running, and not generating any Web errors. In some cases, Microsoft’s Web server produces a Web page even if it has an error. If you don’t check the content of that page, it might look as though the server is actually up and running. As a result, I also have a type of check that simulates a browser visiting the page and reading the content. I use the HttpWebRequest and HttpWebResponse classes, as well as some other network I/O code. The following code loops through some good addresses and one bad address to read the TITLE tag from each page:

ArrayList addrs = new ArrayList();
addrs.Add("http://iis02.northcomp.com");
addrs.Add("http://iis03.northcomp.com");
addrs.Add("http://ncs01.northcomp.com");
addrs.Add("http://blahblahblah.northcomp.com");

foreach (string s in addrs)
{
   try
   {

      HttpWebRequest req   = (HttpWebRequest)WebRequest.Create(s);
      HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
      Stream st            = resp.GetResponseStream();
      StreamReader sr      = new StreamReader(st);
      string buffer        = sr.ReadToEnd();
      int startPos, endPos;
      startPos = buffer.IndexOf("<title>",
         StringComparison.CurrentCultureIgnoreCase) + 7;
      endPos   = buffer.IndexOf("</title>",
         StringComparison.CurrentCultureIgnoreCase);

      string title = buffer.Substring(startPos, endPos - startPos);
      Console.WriteLine("Response code from {0}: {1}", s,
                        resp.StatusCode);
      Console.WriteLine("Page title: {0}", title);
      sr.Close();
      st.Close();
   }
   catch (Exception ex)
   {
      Console.WriteLine("Error connecting to {0}.", s);
      Console.WriteLine("Exception:");
      Console.WriteLine(ex.ToString());
   }

}
Console.WriteLine("Web site check completed.");

The basic construction of the code is similar to my other tips covering Ping and the TcpClient class; however, reading data is somewhat different. I first create an HttpWebRequest by using the WebRequest Create method. I immediately get the HttpWebResponse from the request and start reading the data. I do this by using a regular StreamReader class, and the results are dumped into a string. I then look at the string for the TITLE tag and the end of that tag, and then pull the results into a separate string. The assumption here is that if I see the appropriate TITLE tag, the page is functioning properly. In addition, the StatusCode will display as OK for the first three addresses. The fourth generates an error and is trapped appropriately.

You can modify this code to look for any string in the page. For instance, you might want to verify that the last line or HTML tag on the Web page is present, which would imply that the page completed without any errors.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic. Send him your questions and feedback via e-mail at questions@techniquescentral.com.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read