Error handling of specific HTTP response codes such as 404 Not Found
suggest changeusing System.Net;
...
string serverResponse;
try
{
// Call a method that performs an HTTP request (per the above examples).
serverResponse = PerformHttpRequest();
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
HttpWebResponse response = ex.Response as HttpWebResponse;
if (response != null)
{
if ((int)response.StatusCode == 404) // Not Found
{
// Handle the 404 Not Found error
// ...
}
else
{
// Could handle other response.StatusCode values here.
// ...
}
}
}
else
{
// Could handle other error conditions here, such as WebExceptionStatus.ConnectFailure.
// ...
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents