System.Net.Http.HttpResponseMessage ClassRepresents an HTTP response message, including the status code and data.
In the .NET Framework, the HttpResponseMessage class is a core component for working with HTTP responses when using the HttpClient class. It encapsulates all the information that a server sends back to a client after processing an HTTP request. This includes the HTTP version, status code, reason phrase, headers, and the content of the response.
This class is part of the System.Net.Http namespace, which provides classes for sending HTTP requests and receiving HTTP responses.
public class HttpResponseMessage : IDisposable
object
Implemented interfaces:
This class has no fields.
Gets or sets the HTTP content of the response.
Gets the collection of HTTP response headers.
Gets or sets the HTTP request message that led to this response message.
Gets or sets the status code of the response.
Gets or sets the reason phrase associated with the HTTP status code.
Gets or sets the HTTP message version.
Gets a value indicating whether the status code was successful.
Throws an exception if the response status code indicates failure.
Disposes of the HTTP response message.
Returns a string that represents the current object.
The HttpResponseMessage class is fundamental to consuming HTTP services in .NET. When you use HttpClient.SendAsync() or HttpClient.GetAsync(), the result is an HttpResponseMessage.
Key aspects of HttpResponseMessage:
StatusCode property holds this value.ReasonPhrase property provides this.Headers property.Content property is of type HttpContent.HttpRequestMessage that generated this response is available via the RequestMessage property. This can be useful for debugging or correlating requests and responses.IsSuccessStatusCode property is a convenient way to check if the status code falls within the 2xx range, indicating a successful operation. The EnsureSuccessStatusCode() method automates throwing an exception for non-successful status codes.HttpResponseMessage implements IDisposable because its Content property might hold unmanaged resources (like network streams). It's crucial to dispose of HttpResponseMessage objects to release these resources, typically using a using statement.
Here's a common scenario demonstrating how to use HttpResponseMessage:
using System.Net.Http;
using System.Threading.Tasks;
public class Example
{
public async Task FetchDataAsync()
{
using (var client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync("https://jsonplaceholder.typicode.com/posts/1");
// Check if the request was successful
response.EnsureSuccessStatusCode(); // Throws an exception for non-2xx status codes
// Access response properties
HttpStatusCode statusCode = response.StatusCode;
string reasonPhrase = response.ReasonPhrase;
bool isSuccess = response.IsSuccessStatusCode;
// Read the response content
if (response.Content is not null)
{
string responseBody = await response.Content.ReadAsStringAsync();
System.Console.WriteLine($"Status Code: {statusCode}");
System.Console.WriteLine($"Reason Phrase: {reasonPhrase}");
System.Console.WriteLine($"Response Body: {responseBody}");
}
}
catch (HttpRequestException e)
{
System.Console.WriteLine($"Request exception: {e.Message}");
}
}
}
}
In this example:
HttpClient is created and used to send a GET request.using statement ensures that the HttpClient and the HttpResponseMessage are properly disposed of.EnsureSuccessStatusCode() is called to automatically handle unsuccessful HTTP status codes.StatusCode, ReasonPhrase, and IsSuccessStatusCode are accessed.response.Content.ReadAsStringAsync().