HttpRequestMessage Class
Summary
Represents an HTTP request message.
The HttpRequestMessage class provides a comprehensive way to represent an HTTP request, including its method (GET, POST, PUT, DELETE, etc.), URI, headers, and content. It is the fundamental object used when sending HTTP requests using HttpClient.
Syntax
public class HttpRequestMessage : IDisposable
Members
Constructors
| Name | Description |
|---|---|
HttpRequestMessage() |
Initializes a new instance of the HttpRequestMessage class with a default method of GET and no URI. |
HttpRequestMessage(HttpMethod method, string uri) |
Initializes a new instance of the HttpRequestMessage class with a specified HTTP method and URI. |
HttpRequestMessage(HttpMethod method, Uri uri) |
Initializes a new instance of the HttpRequestMessage class with a specified HTTP method and URI. |
Properties
| Name | Description |
|---|---|
Content |
Gets or sets the HTTP content of the request. |
Headers |
Gets the collection of HTTP headers for the request. |
Method |
Gets or sets the HTTP method for the request. |
RequestUri |
Gets or sets the URI to which the request is sent. |
Version |
Gets or sets the HTTP version used by the request. |
Methods
| Name | Description |
|---|---|
Dispose() |
Releases the unmanaged resources used by the HttpRequestMessage and optionally disposes of the managed resources. |
Dispose(bool disposing) |
Releases the unmanaged resources used by the HttpRequestMessage and optionally disposes of the managed resources. |
Examples
The following example shows how to create and send a POST request using HttpRequestMessage and HttpClient.
using namespace System.Net.Http;
using namespace System.Text;
using namespace System.Threading.Tasks;
public class Example
{
public static async Task SendPostRequest()
{
using (var client = new HttpClient())
{
var postData = new Dictionary<string, string>
{
{ "name", "John Doe" },
{ "job", "Developer" }
};
var jsonContent = System.Text.Json.JsonSerializer.Serialize(postData);
using (var request = new HttpRequestMessage(HttpMethod.Post, "https://reqres.in/api/users"))
{
request.Content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode(); // Throws if HTTP status code is not 2xx
var responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
}
}
Remarks
HttpRequestMessage is immutable once sent. It is recommended to create a new HttpRequestMessage for each request, or ensure that the message is disposed of properly if it is reused.
The Headers property provides access to the request headers, including standard headers like User-Agent and custom headers.
The Content property is of type HttpContent, which abstractly represents the body of the HTTP request. Various implementations of HttpContent exist for different data types (e.g., StringContent, ByteArrayContent, FormUrlEncodedContent).
Requirements
| Assembly | System.Net.Http.dll |
|---|