Keywords: C# | HTTP Status Codes | HttpWebRequest | HttpWebResponse | Network Programming
Abstract: This article provides a comprehensive guide on obtaining HTTP status code numerical values in C# using HttpWebRequest and HttpWebResponse. It explores the underlying implementation of HttpStatusCode enumeration, demonstrates type conversion techniques, and discusses exception handling for 4xx and 5xx status codes with complete code examples and best practices.
Fundamentals of HTTP Status Code Retrieval
In C# network programming, the HttpWebResponse.StatusCode property returns a HttpStatusCode enumeration value. The enumeration is defined in the .NET framework as follows:
public enum HttpStatusCode
{
OK = 200,
Created = 201,
Accepted = 202,
MovedPermanently = 301,
Found = 302,
BadRequest = 400,
Unauthorized = 401,
Forbidden = 403,
NotFound = 404,
InternalServerError = 500,
ServiceUnavailable = 503
}Each enumeration member directly corresponds to the numerical values defined in the HTTP protocol specification, making type conversion the most straightforward approach to obtain numerical status codes.
Practical Methods for Retrieving Status Code Numbers
Numerical HTTP status codes can be obtained through simple type conversion:
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://www.example.com/");
webRequest.AllowAutoRedirect = false;
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
int statusCodeNumber = (int)response.StatusCode;
Console.WriteLine(statusCodeNumber); // Output: 301This method avoids the need for complex switch statements or dictionary mappings by leveraging the underlying numerical characteristics of the enumeration.
Exception Handling for Error Status Codes
When servers return 4xx or 5xx status codes, the GetResponse() method throws a WebException. Proper handling requires catching the exception and retrieving the status code from the exception object:
HttpWebResponse response = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.example.com/");
response = (HttpWebResponse)request.GetResponse();
int statusCode = (int)response.StatusCode;
Console.WriteLine(statusCode);
}
catch (WebException ex)
{
if (ex.Response != null)
{
response = (HttpWebResponse)ex.Response;
int statusCode = (int)response.StatusCode;
Console.WriteLine(statusCode); // Output: 404 or other error codes
}
}
finally
{
response?.Close();
}Status Code Handling in System Design
In complex system designs, HTTP status code processing requires consideration of additional factors. Status codes not only indicate request success or failure but also carry rich semantic information. For instance, 3xx codes indicate redirection, 4xx codes represent client errors, and 5xx codes signify server errors. Proper handling of these status codes is crucial for building robust distributed systems.
In practical applications, it is recommended to encapsulate status code processing logic into independent components that provide unified error handling mechanisms. This approach enhances code maintainability and system stability.
Best Practice Recommendations
1. Always use type conversion instead of string comparison for retrieving status code numbers
2. Implement proper exception handling for network requests, particularly for 4xx and 5xx status codes
3. Release network resources promptly to avoid memory leaks
4. Consider using using statements for automatic resource lifecycle management
5. Establish unified status code processing standards in distributed system designs