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.


C# http status code

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:

using System; using System.Net.Http; using System.Threading.Tasks; namespace HttpClientExample { class Program { static async Task Main(string[] args) { using (var client = new HttpClient()) { try { var url = "https://www.example.com/api/data/1"; // Specify the URL var response = await client.GetAsync(url); // Send the GET request. if (response.IsSuccessStatusCode) // Check the status { var content = await response.Content.ReadAsStringAsync(); // Read the response Console.WriteLine(content); } else { Console.WriteLine("Error: " + response.StatusCode); } } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } } } }

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.