Overview
Represents an HTTP response message including the status code and headers. This class is the base class for all HTTP responses.
The HttpResponseMessage
class provides a comprehensive way to represent an HTTP response received from an HTTP server. It encapsulates all essential information about the response, including the HTTP status code, headers, content, and protocol version.
This class is fundamental for client-side HTTP communication in .NET, allowing developers to inspect and process the results of web requests. It is commonly used in conjunction with the HttpClient
class.
Syntax
public class HttpResponseMessage : IDisposable
Namespace: System.Net.Http
Assembly: System.Net.Http.dll
Implements: IDisposable
Constructors
The HttpResponseMessage
class has the following constructors:
Name | Description |
---|---|
HttpResponseMessage() |
Initializes a new instance of the HttpResponseMessage class with a default null content and null headers. |
HttpResponseMessage(HttpStatusCode statusCode) |
Initializes a new instance of the HttpResponseMessage class with the specified HTTP status code. |
Properties
Name | Type | Description |
---|---|---|
Content |
HttpContent |
Gets or sets the HTTP content of the response. |
Headers |
HttpResponseHeaders |
Gets the HTTP response headers. |
ReasonPhrase |
string |
Gets or sets the reason phrase for the HTTP response status. |
RequestMessage |
HttpRequestMessage |
Gets or sets the originating request message that led to this response message. |
StatusCode |
HttpStatusCode |
Gets or sets the HTTP status code associated with the response. |
Version |
Version |
Gets or sets the protocol version used by the server to generate the response. |
Methods
Name | Description |
---|---|
Dispose() |
Releases the unmanaged resources that are used by the object and, optionally, releases the managed resources. |
EnsureSuccessStatusCode() |
Throws an exception if the response status code indicates an error. |
Dispose(bool disposing) |
Releases the unmanaged resources that are used by the object and, optionally, releases the managed resources. |
Examples
Using HttpResponseMessage with HttpClient
The following example demonstrates how to use HttpClient
to send a GET request and process the HttpResponseMessage
.
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class HttpExample
{
public static async Task Main(string[] args)
{
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync("https://jsonplaceholder.typicode.com/todos/1");
// Ensure the response was successful
response.EnsureSuccessStatusCode();
// Read the response content as a string
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
// Access other properties
Console.WriteLine($"Status Code: {response.StatusCode}");
Console.WriteLine($"Reason Phrase: {response.ReasonPhrase}");
Console.WriteLine($"Protocol Version: {response.Version}");
// You can also inspect headers
foreach (var header in response.Headers)
{
Console.WriteLine($"Header: {header.Key} = {string.Join(", ", header.Value)}");
}
}
catch (HttpRequestException e)
{
Console.WriteLine($"Request exception: {e.Message}");
}
}
}
}