tags:

views:

65

answers:

3

What's the easiest way in .NET to check what status code a web server replies with to a GET request?

Note that I do not need the body of the response. In fact, if possible, only the header should be requested. Having said that, however, if requesting that the body of the response be omitted significantly increases the complexity of the code, receiving the body would be fine.

Also, I'm particularly interested in catching ALL the possible appropriate exceptions (System.Net.WebException, System.IO.IOException, System.Net.Sockets.SocketException, etc.), as this routine will run thousands of times a day.

+2  A: 

If you use HttpWebRequest, it's pretty easy:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://url");
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
HttpStatusCode status = response.StatusCode;

You can surround that with a blanket catch clause, or look at the docs for WebRequest.Create and .GetResponse to see what exceptions will get thrown.

Kirk Woll
+2  A: 

Use the HTTP method HEAD, which is the same as GET except doesn't return the body:

var request = (HttpWebRequest)WebRequest.Create("http://www.example.com");
request.Method = "HEAD";
var response = (HttpWebResponse)request.GetResponse();

// status code...
response.StatusCode;

From Section 9.4 of RFC2616:

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.

John Rasch
+1, Cool, I didn't know about the HEAD command.
Kirk Woll
+3  A: 
public HttpStatusCode GetHeaders(string url)
    {
        HttpStatusCode result = default(HttpStatusCode);

        var request = HttpWebRequest.Create(url);
        request.Method = "HEAD";
        using (var response = request.GetResponse() as HttpWebResponse)
        {
            if (response != null)
            {
                result = response.StatusCode;
                response.Close();
            }
        }

        return result;
    }
Ivan Ferić