HttpWebRequest, HttpWebResponse, and the WebException

Standard

Calling the GetResponse() method on the HttpWebRequest class yields some interesting results. It determines that a status code of anything else but 200 (OK) is an exceptional case and throws a WebException exception.

The following code demonstrates how to return the status code even if the URL in the request is not OK.

HttpStatusCode httpStatusCode;
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://invalidsitename.net/");
try
{
    using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
    {
        httpStatusCode = httpWebResponse.StatusCode; //OK
    }
}
catch (WebException webException)
{
    if (webException.Response != null)
    {
        HttpWebResponse httpWebExceptionResponse = (HttpWebResponse)webException.Response;
        httpStatusCode = httpWebExceptionResponse.StatusCode; //Something other than OK
    }
    else
    {
        throw webException; //Invalid host name
    }
}
return httpStatusCode;

The First Project

Standard

Well, the training was finally over and the start of March brought the start of my first project as a consultant at Avanade.

After a little bit of networking with a fellow WWU grad, I got onto my first project, and wouldn’t you know it, it’s at Microsoft. I am currently working in a small team as a UI developer working with ASP.NET, C#, and SSRS, which is quite an exciting opportunity considering ASP.NET and SQL Server are my bread and butter. There was only one problem.. I (and my trusty new laptop) had to be converted to the Microsoft religion before being able to do pretty much anything. So after a new laptop image, hours in software installs and reinstalls, badge creation, badge recreation, and a smart card reader I was finally fully capable of working at Microsoft.

As developing on this project starts I have decided that I will be expanding the scope of my weblog to not only include life snippets but also some code snippets and developer thoughts that I find useful or interesting along the way.