C# HttpClient status code
The status code of an HTTP response can be accessed through the StatusCode property of the HttpResponseMessage object returned by a request method in C# HttpClient. The status code is a 3-digit HTTP status code that indicates the result of the request. Some common status codes are 200 (OK), 201 (Created), 204 (No Content), 400 (Bad Request), 401 (Unauthorized), 404 (Not Found), 500 (Internal Server Error), etc.
HTTP status codes group
HTTP status codes are grouped into five classes, based on their first digit:
HTTP status codes | Description |
---|---|
Informational (1xx) | The request was received, and the server is continuing to process |
Success (2xx) | The request was successfully received, understood, and accepted. |
Redirection (3xx) | Further action needs to be taken in order to complete the request. |
Client Error (4xx) | Request contains bad syntax or cannot be fulfilled by the server. |
Server Error (5xx) | The server failed to fulfill a valid request. |
Common HTTP status codes
Following are some common HTTP status codes that you might encounter when using C# HttpClient:
HTTP status codes | Description |
---|---|
200 OK | The request was successful and the server has returned the requested data. |
201 Created | The request was successful and the server has created a new resource as a result. |
204 No Content | The request was successful, but there is no representation to return (i.e., the response is empty). |
400 Bad Request | The request was malformed or invalid. |
401 Unauthorized | The request requires authentication, and the client failed to provide valid credentials. |
403 Forbidden | The client does not have permission to access the requested resource. |
404 Not Found | The requested resource could not be found on the server. |
500 Internal Server Error | An error occurred on the server. |
These are just a few examples of HTTP status codes. There are many more codes defined in the HTTP specification. When using C# HttpClient, you can access the status code of an HTTP response through the StatusCode property of the HttpResponseMessage object returned by a request method.
How to check the status code using C# HttpClient
The following example shows how to check the status code of an HTTP response using C# HttpClient:
In the above example, the GetAsync method is used to send a GET request to the specified URL. The method returns a Task of type HttpResponseMessage, which represents the response from the server. The IsSuccessStatusCode property of the HttpResponseMessage object can be used to check if the status code of the response is in the 200-299 range, which indicates a successful response. If the response is not successful, the status code can be accessed through the StatusCode property. The response content can be read as a string using the ReadAsStringAsync method.
- How to send email from C#
- How to send email with attachment from C#
- How to send html email from C#
- How to send cdo email from C#
- How to find hostname of a computer
- How to find IP Adress of a computer
- How to read URL Content from webserver
- How to C# Socket programming
- C# Server Socket program
- C# Client Socket program
- C# Multi threaded socket programming
- C# Multi threaded Server Socket programming
- C# Multi threaded Client Socket programming
- How to C# Chat server programming
- How to C# Chat Server
- How to C# Chat Client
- How to web browser in C#
- No connection could be made because the target machine actively refused it
- System.Net.Sockets.SocketException (0x80004005)
- C# HttpClient - HTTP requests with HttpClient in C#