Search code examples
c#httpexceptionhttpwebrequesthttpwebresponse

Obtaining the status code of an http request when an exception is thrown (in c#)


I am using an instance of the HttpWebRequest class to send a request and receive a response from a website. In case an exception thrown is a WebException, i can retrieve the http status code by typecasting the WebException as ((HttpWebResponse)exc.Response).StatusCode;. How can i do the same for other types of exceptions, such as UriFormatException?


Solution

  • UriFormatException would mean that the URL you're trying to access isn't a valid URL, so there won't be any status code, because there is not a request.