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.



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Targeted attacks and advanced threats are customized to infiltrate your unique IT infrastructure, evade conventional defenses, and remain hidden while stealing your corporate data. To detect these criminal intrusions, analysts and security experts agree that organizations should deploy advanced threat protection as part of an expanded security monitoring strategy. For this comparative analysis of breach detection systems, product analysis reports and comparative analysis reports are used to create the security …

  • On-demand Event Event Date: September 17, 2014 Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this webcast and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the future state of your servers, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds