views:

46

answers:

2

We have a url and we need to check whether web page is active or not. We tried following code: WebResponse objResponse = null;

        WebRequest objRequest = HttpWebRequest.Create(URL);
        objRequest.Method = "HEAD";
        try
        {
            objResponse = objRequest.GetResponse();
            objResponse.Close();                
        }
        catch (Exception ex)
        {

        }

Above code gave exception if unable to get a response but also works fine even if we have a "server error" on that page? Any help how to get server error?

+5  A: 

The HttpResponse class has a StatusCode property which you can check. If it's 200 everything is ok.

You can change your code to this:

        HttpWebResponse objResponse = null;
        var objRequest = HttpWebRequest.Create("http://google.com"); 
        objResponse = (HttpWebResponse) objRequest.GetResponse();
        if(objResponse.StatusCode != HttpStatusCode.OK)
        {
            Console.WriteLine("It failed");
        }else{
            Console.WriteLine("It worked");
        }
klausbyskov
Any resource from where i can have an idea how to cast or change my code?
Malik
@Malik, I have updated my answer with a working example.
klausbyskov
Thanks for the prompt reply but i ma facing the issue that url is returning "statuscode = OK" but when i try to access that url, it gives me yellow screen of death and "Server Error in '/' Application." message. I need to check if the page is up, if up, then check if it "working fine" or "giving yellow screen of death". We need to check something inside else{ Console.WriteLine("It worked"); }.
Malik
@Malik, you may want to turn custom errors off, so that IIS will return an actual http error code instead of a nicely formatted html document containing the error.
klausbyskov
@klausbyskov: I wish we have luxury of turning those errors off. :(
Malik
+2  A: 

For one thing, use a using statement on the response - that way you'll dispose of it whatever happens.

Now, if a WebException is thrown, you can catch that and look at WebException.Response to find out the status code and any data sent back:

WebRequest request = WebRequest.Create(URL);
request.Method = "HEAD";
try
{
    using (WebResponse response = request.GetResponse())
    {
        // Use data for success case
    }
}
catch (WebException ex)
{
    HttpWebResponse errorResponse = (HttpWebResponse) ex.Response;
    HttpStatusCode status = errorResponse.StatusCode;
    // etc
}
Jon Skeet
its working fine in normal scenarios like when page is up or down but we want to verify that if page is up, then check that does it has any server error and it is not displaying yellow screen of death.
Malik
@Malik: Well you'll potentially need to check whether it's displaying a graceful error page with a "success" HTTP status code - but if the HTTP status code is an error, it will go into the catch block. Basically if the yellow screen of death is still a success page, you should check the content.
Jon Skeet
Thanks Jon. I have looked into "myResponse.Headers.AllKeys" and they all are same for "yellow screen of death page" and a "working page". Any help how i can check content?
Malik
@Malik: You may either need to change your error page, or download some actual content (i.e. GET rather than HEAD) to see the difference.
Jon Skeet
@Jon: GET returns source of the page. I should look for specific words in the source?
Malik
@Malik: Quite possibly, yes. After all, that's what a user would do :) Is the app that you're monitoring under your control? Don't forget that the error page could always change on the next deploy...
Jon Skeet
@Jon: We have a window service that checks whether specific web pages are working or not. We did this using above mentioned code but now if a web page is showing "YSOD", it is marked as "working". We want know to chage that and mark web pages with "YSOD" as "not working". If the web page is changed and started working on next deploy, instead of "YSOD", we will change there status from "Not working" to "working". Should I look for words like - Server Error in '/' Application. / Description / Exception Details / Source Error / Stack Trace / Version Information in the page source.
Malik