HttpRequestMessage Class
Represents an HTTP request message.
public sealed class HttpRequestMessage
Syntax
public sealed class HttpRequestMessage
Namespace: System.Net.Http
Assembly: System.Net.Http.dll
Remarks
The HttpRequestMessage class represents an HTTP request. You typically use this class to construct a request that will be sent by an HttpClient object.
The HttpRequestMessage object provides properties to set the request method (e.g., GET, POST), request URI, headers, and content.
Key properties include:
Method: The HTTP method (e.g., GET, POST, PUT, DELETE).RequestUri: The Uniform Resource Identifier (URI) of the request.Headers: The HTTP headers for the request.Content: The HTTP content for the request, typically used for POST or PUT requests.
When you create an HttpRequestMessage, you can optionally provide the HTTP method and the request URI to its constructor.
Constructors
| Name | Description |
|---|---|
HttpRequestMessage() |
Initializes a new instance of the HttpRequestMessage class. |
HttpRequestMessage(HttpMethod method, string requestUri) |
Initializes a new instance of the HttpRequestMessage class with a specific HTTP method and request URI. |
HttpRequestMessage(HttpMethod method, Uri requestUri) |
Initializes a new instance of the HttpRequestMessage class with a specific HTTP method and request URI. |
Properties
| Name | Type | Description |
|---|---|---|
Content |
HttpContent? |
Gets or sets the HTTP content of the request. |
Headers |
HttpRequestHeaders |
Gets the collection of HTTP request headers. |
Method |
HttpMethod |
Gets or sets the HTTP method for the request. |
Options |
ICollection<KeyValuePair<string, object>> |
Gets a collection of optional parameters that send additional information to the server. |
Properties |
ICollection<KeyValuePair<string, object>> |
Gets a collection of arbitrary optional parameters that use the request as a context. |
RequestUri |
Uri? |
Gets or sets the Uniform Resource Identifier (URI) of the request. |
Version |
Version |
Gets or sets the HTTP protocol version used for the request. |
VersionPolicy |
HttpVersionPolicy |
Gets or sets the version policy to use for the request. |
Methods
| Name | Description |
|---|---|
Dispose() |
Releases the unmanaged resources that are used by the HttpRequestMessage and optionally releases the managed resources. |
Dispose(bool disposing) |
Releases the unmanaged resources that are used by the HttpRequestMessage and optionally releases the managed resources. |
ToString() |
Returns a string that represents the current object. |
Examples
Creating a simple GET request
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class Example
{
public static async Task MakeGetRequest()
{
// Create an HttpRequestMessage with GET method and a URI
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://www.example.com/api/data"))
{
// You can add custom headers if needed
request.Headers.Add("Accept", "application/json");
// Use HttpClient to send the request
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("Response Body: " + responseBody);
}
else
{
Console.WriteLine($"Error: {response.StatusCode}");
}
}
}
}
}
Creating a POST request with content
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
public class Example
{
public static async Task MakePostRequest()
{
// Create an HttpRequestMessage with POST method and a URI
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://www.example.com/api/users"))
{
// Define the content to send
string jsonPayload = @"{
""name"": ""John Doe"",
""email"": ""john.doe@example.com""
}";
// Create HttpContent from the string
HttpContent content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
request.Content = content;
// Add custom headers
request.Headers.Add("X-Custom-Header", "MyValue");
// Use HttpClient to send the request
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("Response Body: " + responseBody);
}
else
{
Console.WriteLine($"Error: {response.StatusCode}");
}
}
}
}
}
Inheritance
Object > HttpRequestMessage
Implements
IDisposable
Attributes
This class has no attributes.
Thread Safety
Public static members of this type are thread-safe. Any instance members are not guaranteed to be thread-safe.
Requirements
.NET: Supported in: 7, 6, 5, 4.8, 4.7.2, 4.7.1, 4.7, 4.6.2, 4.6.1, 4.6, 4.5.2, 4.5.1, 4.5, 3.5
.NET Framework: Supported in: 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8