HttpRequestMessage

.NET Class Library

HttpRequestMessage Class

System.Net.Http System.Net.Http.dll

Represents an HTTP request message.

Constructors

public HttpRequestMessage()

Initializes a new instance of the HttpRequestMessage class.

public HttpRequestMessage(HttpMethod method, string requestUri)

Initializes a new instance of the HttpRequestMessage class with a specific HTTP method and request URI.

  • method: The HTTP method to use for the request.

  • requestUri: The Uniform Resource Identifier (URI) to send the request to.

public HttpRequestMessage(HttpMethod method, Uri requestUri)

Initializes a new instance of the HttpRequestMessage class with a specific HTTP method and request URI.

  • method: The HTTP method to use for the request.

  • requestUri: The Uniform Resource Identifier (URI) to send the request to.

Properties

public HttpMethod Method { get; set; }

The HTTP method used for the request. This property is required.

public Uri RequestUri { get; set; }

The Uniform Resource Identifier (URI) for the request. This property is required.

public HttpContent Content { get; set; }

The HTTP content to send to the server. This is null for GET requests.

public HttpRequestMessageHeaders Headers { get; }

Gets the HTTP headers to send with the request.

public Dictionary Properties { get; }

Gets a collection of objects that can be used to store and retrieve custom data for the request. These are internal properties that are not sent to the server.

public CancellationTokenSource GetCancellationTokenSource()

Returns the CancellationTokenSource associated with this request.

Methods

public void Dispose()

Releases the unmanaged resources that are used by the HttpRequestMessage and optionally disposes of the managed resources.

protected virtual void Dispose(bool disposing)

Releases the unmanaged resources that are used by the HttpRequestMessage and optionally disposes of the managed resources.

public override string ToString()

Returns a string that represents the current object.

Remarks

The HttpRequestMessage class is used to represent an HTTP request. It can be used to send requests to a web server using the HttpClient class.

To create an HTTP request, you typically instantiate HttpRequestMessage and set its Method, RequestUri, and optionally Content properties. The Headers property can be used to add custom HTTP headers.

When you are finished with an HttpRequestMessage object, you should call its Dispose() method to release any unmanaged resources.

Example

The following example shows how to create and send a GET request using HttpRequestMessage and HttpClient.


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

public class Example
{
    public static async Task Main(string[] args)
    {
        using (var httpClient = new HttpClient())
        {
            // Create an HttpRequestMessage
            var request = new HttpRequestMessage
            {
                Method = HttpMethod.Get,
                RequestUri = new Uri("https://jsonplaceholder.typicode.com/todos/1")
            };

            // Add a custom header
            request.Headers.Add("X-Custom-Header", "MyValue");

            // Send the request and get the response
            HttpResponseMessage response = await httpClient.SendAsync(request);

            // Check if the request was successful
            if (response.IsSuccessStatusCode)
            {
                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseBody);
            }
            else
            {
                Console.WriteLine($"Error: {response.StatusCode}");
            }
        }
    }
}