4

I have a ASP.Net 3.5 Web app (C#) where I need to programmatically check to see if another one of our sites (that isn't ASP.Net) is up and running. Currently, I have a method with the following code which checks for a StatusCode of 200. The issue I'm running into is that the IIS7 splash page that comes up returns a status code of 200, and I don't see anything else within the response object that would allow me to verify the page we are expecting actually displays. I would like to avoid getting the response back and using a StreamReader just to look for a div on the page to verify it's valid (if possible) as they do (similarly) in this link.

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlToCheck);
        request.AllowAutoRedirect = true;

        HttpWebResponse response;
        try
        {
            response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                return true;
            }
            return false;
        }
        catch (Exception)
        {
            return false;
        }

Any help is greatly appreciated.

Community
  • 1
  • 1
shakin
  • 171
  • 15
  • 1
    What is your reason for not wanting to read the response? You've already made the request and received the response back - it's only a matter of 5 or 6 lines of code to read the response and look for an indication that it's what you expect. – Joe Enos Jun 06 '13 at 14:32
  • @JoeEnos For some reason I was thinking I needed to make another request to get that information, but you're correct. Thanks! – shakin Jun 06 '13 at 14:53
  • 1
    No prob. I've always found the `HttpWebRequest/HttpWebResponse` syntax a little weird - it does kind of look like you're doing something extra when you read the response stream, when you're really not. Some of the other alternatives (`WebClient` and the newer `HttpClient`) do a better job of easily reading the response. – Joe Enos Jun 06 '13 at 14:56

2 Answers2

5

Just read the Response it's already there and waiting!

Stream s = response.GetResponseStream();
StreamReader r = new StreamReader(s);
string html = r.ReadToEnd();

// IIS7
if(html.Contains(@"<div id=""container"">
<a href=""http://go.microsoft.com/fwlink/?linkid=66138&amp;clcid=0x409""><img src=""welcome.png"" alt=""IIS7"" width=""571"" height=""411""></a>
</div>") {


}
Gabe
  • 49,577
  • 28
  • 142
  • 181
  • Yeah, that was a "duh" moment. Thanks! I may adjust the code to check for our page though because our clients can use IIS6 or IIS7. Plus, in case there are any edge cases that occur it will handle those as well. – shakin Jun 06 '13 at 14:55
  • Yea, this was just a simple example, based up which version of IIS you will have to adjust what you're searchin for. Glad it helped – Gabe Jun 06 '13 at 14:58
0

Investigate the headers that are returned by the other application. Most probably it will have some additional headers present compared to the IIS splash screen. For example, it might specify Cache-Control. If your response does not have that header, you can expect it to be the splash screen.

Knaģis
  • 20,827
  • 7
  • 66
  • 80