Table of Contents

Class UriExtensions

Namespace
Cuemon.Extensions.Net.Http
Assembly
Cuemon.Extensions.Net.dll

Extension methods for the Uri struct.

public static class UriExtensions
Inheritance
UriExtensions

Examples

UriExtensions provides HTTP extension methods directly on Uri covering GET, POST, PUT, DELETE, HEAD, OPTIONS, PATCH, and TRACE with support for custom headers, content types, and cancellation. This example assigns a stub IHttpClientFactory that returns a fixed 200 OK response, then calls each HTTP method variant including HttpGetAsync, HttpDeleteAsync, HttpPostAsync with JSON content, HttpPutAsync with a stream body, HttpPatchAsync, HttpTraceAsync, and the generic HttpAsync overload for full HttpRequestOptions control with custom headers. It also demonstrates passing a CancellationToken with a timeout that triggers a TaskCanceledException. Console output confirms that each request returns 200 OK and that cancellation is handled gracefully.

using System.Text;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Cuemon.Extensions.Net.Http;

namespace MyApp.Http
{
    public class UriExtensionsExample
    {
        public async Task DemonstrateAsync()
        {
            // Configure a test HTTP client factory that returns predictable responses
            UriExtensions.DefaultHttpClientFactory = new StubHttpClientFactory(HttpStatusCode.OK);

            var location = new Uri("https://example.com/api/items");

            // GET request
            using (var response = await location.HttpGetAsync())
            {
                Console.WriteLine($"GET: {response.StatusCode}"); // 200
            }

            // DELETE request
            using (var response = await location.HttpDeleteAsync())
            {
                Console.WriteLine($"DELETE: {response.StatusCode}"); // 200
            }

            // HEAD request
            using (var response = await location.HttpHeadAsync())
            {
                Console.WriteLine($"HEAD: {response.StatusCode}"); // 200
            }

            // OPTIONS request
            using (var response = await location.HttpOptionsAsync())
            {
                Console.WriteLine($"OPTIONS: {response.StatusCode}"); // 200
            }

            // POST with string content type
            using (var response = await location.HttpPostAsync(
                "application/json", ToStream("{\"name\":\"New Item\"}")))
            {
                Console.WriteLine($"POST: {response.StatusCode}"); // 200
            }

            // POST with MediaTypeHeaderValue
            using (var response = await location.HttpPostAsync(
                MediaTypeHeaderValue.Parse("application/json; charset=utf-8"),
                ToStream("{\"name\":\"Another\"}")))
            {
                Console.WriteLine($"POST (typed): {response.StatusCode}"); // 200
            }

            // PUT
            using (var response = await location.HttpPutAsync(
                "application/json", ToStream("{\"name\":\"Updated\"}")))
            {
                Console.WriteLine($"PUT: {response.StatusCode}"); // 200
            }

            // PATCH
            using (var response = await location.HttpPatchAsync(
                "application/json-patch+json", ToStream("[{\"op\":\"replace\",\"path\":\"/name\",\"value\":\"Patched\"}]")))
            {
                Console.WriteLine($"PATCH: {response.StatusCode}"); // 200
            }

            // TRACE
            using (var response = await location.HttpTraceAsync())
            {
                Console.WriteLine($"TRACE: {response.StatusCode}"); // 200
            }

            // Generic HTTP method with string content type
            using (var response = await location.HttpAsync(
                HttpMethod.Post, "text/plain", ToStream("Hello")))
            {
                Console.WriteLine($"Generic POST: {response.StatusCode}"); // 200
            }

            // Generic HTTP method with typed content type
            using (var response = await location.HttpAsync(
                HttpMethod.Put,
                MediaTypeHeaderValue.Parse("application/octet-stream"),
                ToStream("binary")))
            {
                Console.WriteLine($"Generic PUT: {response.StatusCode}"); // 200
            }

            // Full control via HttpRequestOptions
            using (var response = await location.HttpAsync(o =>
            {
                o.Request.Method = HttpMethod.Get;
                o.Request.Headers.Add("X-Custom", "my-value");
                o.CancellationToken = CancellationToken.None;
            }))
            {
                Console.WriteLine($"Custom GET: {response.StatusCode}"); // 200
            }

            // Use cancellation token
            using var cts = new CancellationTokenSource();
            cts.CancelAfter(TimeSpan.FromSeconds(5));
            try
            {
                using var response = await location.HttpGetAsync(cts.Token);
                Console.WriteLine($"Cancellable GET: {response.StatusCode}");
            }
            catch (TaskCanceledException)
            {
                Console.WriteLine("Request was cancelled.");
            }
        }

        private static Stream ToStream(string value)
        {
            var bytes = Encoding.UTF8.GetBytes(value);
            return new MemoryStream(bytes);
        }
    }

    /// <summary>
    /// A stub factory that returns an HttpClient backed by a handler
    /// that returns a fixed status code for all requests.
    /// </summary>
    internal class StubHttpClientFactory : IHttpClientFactory
    {
        private readonly HttpStatusCode _statusCode;

        public StubHttpClientFactory(HttpStatusCode statusCode)
        {
            _statusCode = statusCode;
        }

        public HttpClient CreateClient(string name)
        {
            return new HttpClient(new StubHttpMessageHandler(_statusCode));
        }
    }

    internal class StubHttpMessageHandler : HttpMessageHandler
    {
        private readonly HttpStatusCode _statusCode;

        public StubHttpMessageHandler(HttpStatusCode statusCode)
        {
            _statusCode = statusCode;
        }

        protected override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken ct)
        {
            var response = new HttpResponseMessage(_statusCode)
            {
                Content = new StringContent("OK"),
                RequestMessage = request
            };
            return Task.FromResult(response);
        }
    }
}

Properties

DefaultHttpClientFactory

Gets or sets the default IHttpClientFactory implementation for the extensions methods on this class.

public static IHttpClientFactory DefaultHttpClientFactory { get; set; }

Property Value

IHttpClientFactory

The default IHttpClientFactory implementation for the URI extensions methods on this class.

Methods

HttpAsync(Uri, Action<HttpRequestOptions>)

Send a request as an asynchronous operation.

public static Task<HttpResponseMessage> HttpAsync(this Uri location, Action<HttpRequestOptions> setup)

Parameters

location Uri

The Uri to extend.

setup Action<HttpRequestOptions>

The HttpRequestOptions which need to be configured.

Returns

Task<HttpResponseMessage>

The task object representing the asynchronous operation.

Exceptions

ArgumentNullException

location cannot be null.

HttpAsync(Uri, HttpMethod, MediaTypeHeaderValue, Stream, CancellationToken)

Send a request as an asynchronous operation.

public static Task<HttpResponseMessage> HttpAsync(this Uri location, HttpMethod method, MediaTypeHeaderValue contentType, Stream content, CancellationToken ct = default)

Parameters

location Uri

The Uri to extend.

method HttpMethod

The HTTP method.

contentType MediaTypeHeaderValue

The Content-Type header of the HTTP request sent to the server.

content Stream

The HTTP request content sent to the server.

ct CancellationToken

The cancellation token to cancel operation.

Returns

Task<HttpResponseMessage>

The task object representing the asynchronous operation.

Exceptions

ArgumentNullException

location cannot be null -or- method cannot be null -or- contentType cannot be null -or- content cannot be null.

HttpAsync(Uri, HttpMethod, string, Stream, CancellationToken)

Send a request as an asynchronous operation.

public static Task<HttpResponseMessage> HttpAsync(this Uri location, HttpMethod method, string contentType, Stream content, CancellationToken ct = default)

Parameters

location Uri

The Uri to extend.

method HttpMethod

The HTTP method.

contentType string

The Content-Type header of the HTTP request sent to the server.

content Stream

The HTTP request content sent to the server.

ct CancellationToken

The cancellation token to cancel operation.

Returns

Task<HttpResponseMessage>

The task object representing the asynchronous operation.

Exceptions

ArgumentNullException

location cannot be null -or- method cannot be null -or- contentType cannot be null -or- content cannot be null.

HttpDeleteAsync(Uri, CancellationToken)

Send a DELETE request to the specified Uri as an asynchronous operation.

public static Task<HttpResponseMessage> HttpDeleteAsync(this Uri location, CancellationToken ct = default)

Parameters

location Uri

The Uri to extend.

ct CancellationToken

The cancellation token to cancel operation.

Returns

Task<HttpResponseMessage>

The task object representing the asynchronous operation.

Exceptions

ArgumentNullException

location cannot be null.

HttpGetAsync(Uri, CancellationToken)

Send a GET request to the specified Uri as an asynchronous operation.

public static Task<HttpResponseMessage> HttpGetAsync(this Uri location, CancellationToken ct = default)

Parameters

location Uri

The Uri to extend.

ct CancellationToken

The cancellation token to cancel operation.

Returns

Task<HttpResponseMessage>

The task object representing the asynchronous operation.

Exceptions

ArgumentNullException

location cannot be null.

HttpHeadAsync(Uri, CancellationToken)

Send a HEAD request to the specified Uri as an asynchronous operation.

public static Task<HttpResponseMessage> HttpHeadAsync(this Uri location, CancellationToken ct = default)

Parameters

location Uri

The Uri to extend.

ct CancellationToken

The cancellation token to cancel operation.

Returns

Task<HttpResponseMessage>

The task object representing the asynchronous operation.

Exceptions

ArgumentNullException

location cannot be null.

HttpOptionsAsync(Uri, CancellationToken)

Send an OPTIONS request to the specified Uri as an asynchronous operation.

public static Task<HttpResponseMessage> HttpOptionsAsync(this Uri location, CancellationToken ct = default)

Parameters

location Uri

The Uri to extend.

ct CancellationToken

The cancellation token to cancel operation.

Returns

Task<HttpResponseMessage>

The task object representing the asynchronous operation.

Exceptions

ArgumentNullException

location cannot be null.

HttpPatchAsync(Uri, MediaTypeHeaderValue, Stream, CancellationToken)

Send a PATCH request to the specified Uri as an asynchronous operation.

public static Task<HttpResponseMessage> HttpPatchAsync(this Uri location, MediaTypeHeaderValue contentType, Stream content, CancellationToken ct = default)

Parameters

location Uri

The Uri to extend.

contentType MediaTypeHeaderValue

The Content-Type header of the HTTP request sent to the server.

content Stream

The HTTP request content sent to the server.

ct CancellationToken

The cancellation token to cancel operation.

Returns

Task<HttpResponseMessage>

The task object representing the asynchronous operation.

Exceptions

ArgumentNullException

location cannot be null.

HttpPatchAsync(Uri, string, Stream, CancellationToken)

Send a PATCH request to the specified Uri as an asynchronous operation.

public static Task<HttpResponseMessage> HttpPatchAsync(this Uri location, string contentType, Stream content, CancellationToken ct = default)

Parameters

location Uri

The Uri to extend.

contentType string

The Content-Type header of the HTTP request sent to the server.

content Stream

The HTTP request content sent to the server.

ct CancellationToken

The cancellation token to cancel operation.

Returns

Task<HttpResponseMessage>

The task object representing the asynchronous operation.

Exceptions

ArgumentNullException

location cannot be null.

HttpPostAsync(Uri, MediaTypeHeaderValue, Stream, CancellationToken)

Send a POST request to the specified Uri as an asynchronous operation.

public static Task<HttpResponseMessage> HttpPostAsync(this Uri location, MediaTypeHeaderValue contentType, Stream content, CancellationToken ct = default)

Parameters

location Uri

The Uri to extend.

contentType MediaTypeHeaderValue

The Content-Type header of the HTTP request sent to the server.

content Stream

The HTTP request content sent to the server.

ct CancellationToken

The cancellation token to cancel operation.

Returns

Task<HttpResponseMessage>

The task object representing the asynchronous operation.

Exceptions

ArgumentNullException

location cannot be null.

HttpPostAsync(Uri, string, Stream, CancellationToken)

Send a POST request to the specified Uri as an asynchronous operation.

public static Task<HttpResponseMessage> HttpPostAsync(this Uri location, string contentType, Stream content, CancellationToken ct = default)

Parameters

location Uri

The Uri to extend.

contentType string

The Content-Type header of the HTTP request sent to the server.

content Stream

The HTTP request content sent to the server.

ct CancellationToken

The cancellation token to cancel operation.

Returns

Task<HttpResponseMessage>

The task object representing the asynchronous operation.

Exceptions

ArgumentNullException

location cannot be null.

HttpPutAsync(Uri, MediaTypeHeaderValue, Stream, CancellationToken)

Send a PUT request to the specified Uri as an asynchronous operation.

public static Task<HttpResponseMessage> HttpPutAsync(this Uri location, MediaTypeHeaderValue contentType, Stream content, CancellationToken ct = default)

Parameters

location Uri

The Uri to extend.

contentType MediaTypeHeaderValue

The Content-Type header of the HTTP request sent to the server.

content Stream

The HTTP request content sent to the server.

ct CancellationToken

The cancellation token to cancel operation.

Returns

Task<HttpResponseMessage>

The task object representing the asynchronous operation.

Exceptions

ArgumentNullException

location cannot be null.

HttpPutAsync(Uri, string, Stream, CancellationToken)

Send a PUT request to the specified Uri as an asynchronous operation.

public static Task<HttpResponseMessage> HttpPutAsync(this Uri location, string contentType, Stream content, CancellationToken ct = default)

Parameters

location Uri

The Uri to extend.

contentType string

The Content-Type header of the HTTP request sent to the server.

content Stream

The HTTP request content sent to the server.

ct CancellationToken

The cancellation token to cancel operation.

Returns

Task<HttpResponseMessage>

The task object representing the asynchronous operation.

Exceptions

ArgumentNullException

location cannot be null.

HttpTraceAsync(Uri, CancellationToken)

Send a TRACE request to the specified Uri as an asynchronous operation.

public static Task<HttpResponseMessage> HttpTraceAsync(this Uri location, CancellationToken ct = default)

Parameters

location Uri

The Uri to extend.

ct CancellationToken

The cancellation token to cancel operation.

Returns

Task<HttpResponseMessage>

The task object representing the asynchronous operation.

Exceptions

ArgumentNullException

location cannot be null.