Getting Http Status code number 200, 301, 404, etc. from HttpWebRequest and HttpWebResponse
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Understanding HTTP Status Codes with HttpWebRequest and HttpWebResponse
When developing web applications, handling HTTP requests and responses is a fundamental task. HttpWebRequest and HttpWebResponse are .NET classes used for interacting with HTTP servers. This article dives deep into obtaining HTTP status codes from these classes, which are essential for determining the outcome of HTTP requests. We will also explore examples and precautions for effective and efficient HTTP communication.
Introduction to HTTP Status Codes
HTTP status codes are three-digit responses provided by a server indicating the outcome of an HTTP request. They are categorized into five groups:
- 1xx (Informational): Request received, continuing process.
- 2xx (Successful): The action was successfully received, understood, and accepted. For instance,
200means OK. - 3xx (Redirection): Further action must be taken to complete the request, e.g.,
301for Moved Permanently. - 4xx (Client Error): The request contains bad syntax or cannot be fulfilled, like
404for Not Found. - 5xx (Server Error): The server failed to fulfill a valid request. For example,
500for Internal Server Error.
Retrieving HTTP Status Codes with HttpWebRequest and HttpWebResponse
In .NET, HttpWebRequest is used to send HTTP requests, and HttpWebResponse is used to receive the response from the server. Below is a method to illustrate how to obtain the HTTP status code from an HTTP request:
Explanation of Example Code
- HttpWebRequest Object: This object is created using the
WebRequest.Create(url)method whereurlis the requested resource's URI. - HttpWebResponse Object: The
GetResponse()method sends the request and returnsHttpWebResponse, which represents the response from the server. - HttpStatusCode Property: This property of
HttpWebResponseis used to get the status code of the response, cast into an integer for readability. - Exception Handling: A
WebExceptioncould be thrown due to network issues, DNS errors, or invalid responses. The status code can be extracted fromWebExceptionif the server responds with an error.
Common HTTP Status Codes and Their Meaning
| Status Code | Meaning | Category |
| 200 | OK | Successful |
| 301 | Moved Permanently | Redirection |
| 302 | Found | Redirection |
| 304 | Not Modified | Redirection ( cache ) |
| 400 | Bad Request | Client Error |
| 401 | Unauthorized | Client Error |
| 403 | Forbidden | Client Error |
| 404 | Not Found | Client Error |
| 500 | Internal Server Error | Server Error |
| 502 | Bad Gateway | Server Error |
| 503 | Service Unavailable | Server Error |
Enhancing HTTP Requests with Additional Functionality
- Timeout Settings: Adjust timeout values to manage long-running requests using
request.Timeout. - Custom Headers: Add headers with
request.Headers.Add("Header-Name", "Header-Value")for additional request specifics. - Request Method: Change the request method (e.g., GET, POST) by setting
request.Method.
Best Practices and Considerations
- Error Handling: Adequately handle exceptions and network errors to ensure robust applications.
- Security: Use HTTPS instead of HTTP to encrypt data.
- Resource Management: Always close
HttpWebResponseobjects using ausingstatement to free up system resources. - Performance Monitoring: Monitor load time and status codes for potential optimization.
Conclusion
Handling HTTP status codes through HttpWebRequest and HttpWebResponse is critical in ensuring that your application's server communication runs smoothly. By understanding these codes, developers can implement appropriate responses and error-handling routines, ensuring high application resilience and a good user experience.

