HttpResponseMessage

Represents an HTTP response message received from an HTTP server.

Namespace

System.Net.Http

Summary

Provides classes for sending HTTP requests and receiving HTTP responses.

Remarks

The HttpResponseMessage class represents an HTTP response, including status code, headers, and content. It is typically returned by an HttpClient instance after sending an HttpRequestMessage.

You can access various properties of the response, such as:

Constructors

Public Constructors

HttpResponseMessage()

Initializes a new instance of the HttpResponseMessage class.

HttpResponseMessage(System.Net.HttpStatusCode statusCode)

Initializes a new instance of the HttpResponseMessage class with a specified HTTP status code.

Parameters:
  • statusCode: The System.Net.HttpStatusCode to set on the response.

Properties

Public Properties

Content : System.Net.Http.HttpContent

Gets or sets the HTTP content of the response.

Headers : System.Net.Http.Headers.HttpResponseHeaders

Gets the collection of HTTP response headers.

RequestMessage : System.Net.Http.HttpRequestMessage

Gets or sets the HTTP request message that led to this response message.

ReasonPhrase : System.String

Gets or sets the reason phrase for the HTTP response status code.

RequestMessage : System.Net.Http.HttpRequestMessage

Gets or sets the HTTP request message that led to this response message.

StatusCode : System.Net.HttpStatusCode

Gets or sets the HTTP status code of the response.

Version : System.Version

Gets or sets the HTTP message version.

Methods

Public Methods

Dispose()

Releases the unmanaged resources used by the current instance of the HttpResponseMessage class and optionally disposes of the managed resources.

EnsureSuccessStatusCode() : System.Void

Throws an exception if the response was not successful.

EnsureSuccessStatusCode() : System.Void

Throws an exception if the response was not successful. The exception contains the HttpResponseMessage.

ToString() : System.String

Returns a string that represents the current object.

Example Usage


using System;
using System.Net.Http;
using System.Threading.Tasks;

public class Example
{
    public static async Task MakeRequestAsync()
    {
        using (HttpClient client = new HttpClient())
        {
            try
            {
                HttpResponseMessage response = await client.GetAsync("https://jsonplaceholder.typicode.com/todos/1");
                response.EnsureSuccessStatusCode(); // Throws if the status code is not success

                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseBody);

                Console.WriteLine($"Status Code: {response.StatusCode}");
                Console.WriteLine($"Reason Phrase: {response.ReasonPhrase}");
                Console.WriteLine($"HTTP Version: {response.Version}");

                foreach (var header in response.Headers)
                {
                    Console.WriteLine($"Header: {header.Key} = {string.Join(", ", header.Value)}");
                }
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine($"Request error: {e.Message}");
            }
        }
    }
}
            

See Also