PlainHttp 2.0.0-pre1

This is a prerelease version of PlainHttp.
There is a newer version of this package available.
See the version list below for details.
dotnet add package PlainHttp --version 2.0.0-pre1                
NuGet\Install-Package PlainHttp -Version 2.0.0-pre1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PlainHttp" Version="2.0.0-pre1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PlainHttp --version 2.0.0-pre1                
#r "nuget: PlainHttp, 2.0.0-pre1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PlainHttp as a Cake Addin
#addin nuget:?package=PlainHttp&version=2.0.0-pre1&prerelease

// Install PlainHttp as a Cake Tool
#tool nuget:?package=PlainHttp&version=2.0.0-pre1&prerelease                

PlainHttp NuGet License

An easy HTTP client for .NET 6+ with support for serialization, deserialization, proxies, testing, and more.

Features

  • Wraps HttpClient and provides a cleaner and easier interface
  • Supports any HTTP method
  • Per-request timeout with an actual HttpRequestTimeoutException
  • Per-request proxy with transparent pooling
  • Built-in serialization of objects to JSON/XML/URL-encoded, extensible to any other format
  • Built-in deserialization of JSON/XML responses
  • Download files to disk
  • Read responses with specific response encodings
  • Automatically enabled decompression of responses (all algorithms supported by .NET, i.e. gzip, DEFLATE, and Brotli)
  • Proper pooling and connection lifetime defaults to avoid DNS and socket exhaustion issues
  • Allows to mock requests for unit testing
  • Heavily used in production by @trackbotpro to send millions of requests per day

Supported frameworks

This library targets .NET 6 (LTS) because it requires the PooledConnectionLifetime property on SocketsHttpHandler, introduced in .NET Core 2.2.

This makes sure that reusing the same HttpClient for a long time doesn't have unintended consequences affecting DNS resolution. This library in fact keeps a pool of HttpClient instances that are never disposed.

In particular, the library keeps:

  • One HttpClient per request host
  • One HttpClient per proxy URI (including credentials)

There is currently no mechanism that disposes HttpClient instances that are unused, so if you use a lot of random proxies or many different hostnames, you might get into trouble. This can be easily improved by creating a custom IHttpClientFactory, and then setting the factory instance to the static HttpClientFactory property.

Installation

Install the PlainHttp NuGet package:

dotnet add package PlainHttp

Upgrading from 1.x to 2.x

See the release notes for v2.0.0.

Usage

Basic usage

Basic GET request:

string url = "http://random.org";
IHttpRequest request = new HttpRequest(url);
IHttpResponse response = await request.SendAsync();
string body = await response.ReadString();

Also with Uri:

Uri uri = new Uri("http://random.org");
IHttpRequest request = new HttpRequest(uri);

Error handling

Checking if the HTTP status code is in the 2xx range:

IHttpResponse response = await request.SendAsync();

if (!response.Succeeded)
{
    Console.WriteLine($"Response status code is {response.StatusCode}");
}
else
{
    Console.WriteLine($"Successful response in {response.ElapsedMilliseconds} ms");
}

Asserting that the HTTP status code is in the 2xx range:

IHttpResponse response = await request.SendAsync();
response.EnsureSuccessStatusCode(); // may throw HttpRequestException

Every exception is wrapped in an HttpRequestException, from which HttpRequestTimeoutException is derived:

try
{
    IHttpResponse response = await request.SendAsync();
}
catch (HttpRequestException ex)
{
    if (ex is HttpRequestTimeoutException)
    {
        Console.WriteLine("Request timed out");
    }
    else
    {
        Console.WriteLine("Something bad happened: {0}", ex);
        // PlainHttp.HttpRequestException: Failed request: [GET https://yyyy.org/] [No such host is known] ---> System.Net.Http.HttpRequestException: No such host is known ---> System.Net.Sockets.SocketException: No such host is known
        // etc.
    }
}

Request customization

Setting custom headers:

IHttpRequest request = new HttpRequest(url)
{
    Headers = new Dictionary<string, string>
    {
        // No user agent is set by default
        { "User-Agent", "PlainHttp/1.0" }
    }
};

Request a specific HTTP version to be used. If it's not supported, the default HttpVersionPolicy applies (downgrade to a lower version).

IHttpRequest request = new HttpRequest(url)
{
    Version = new Version(2, 0) // HTTP/2
};

Custom timeout (by default no timeout is set):

IHttpRequest request = new HttpRequest(url)
{
    Timeout = TimeSpan.FromSeconds(10)
};

Request serialization

POST request with URL-encoded payload:

IHttpRequest request = new HttpRequest(url)
{
    Method = HttpMethod.Post,
    Payload = new FormUrlEncodedPayload(new
    {
        hello = "world",
        buuu = true
    })
};

POST request with JSON payload (powered by System.Text.Json):

IHttpRequest request = new HttpRequest(url)
{
    Method = HttpMethod.Post,
    Payload = new JsonPayload(new
    {
        hello = "world"
    })
};

You can pass JsonSerializerOptions with the second argument of JsonPayload.

If you already have a JSON-serialized string, just pass it directly:

IHttpRequest request = new HttpRequest(url)
{
    Method = HttpMethod.Post,
    Payload = new JsonPayload("{ \"key\": true }")
};

POST request with XML payload (powered by System.Xml.Serialization.XmlSerializer):

IHttpRequest request = new HttpRequest(url)
{
    Method = HttpMethod.Post,
    Payload = new XmlPayload(new
    {
        something = "web"
    })
};

You can pass XML serialization options with the second argument of XmlPayload (XmlWriterSettings). If you already have an XML-serialized string, just pass it directly.

POST request with plain text payload:

IHttpRequest request = new HttpRequest(url)
{
    Method = HttpMethod.Post,
    Payload = new PlainTextPayload("plain text")
};

Response deserialization

To read the response body as a string:

string body = await response.ReadString();

Optionally, you can specify the encoding to use:

string body = await response.ReadString(Encoding.GetEncoding("ISO-8859-1"));

To read the body as a stream:

Stream stream = await response.ReadStream();

Note that when using ReadStream the response message is not automatically disposed, so you must take care of disposing it manually when you're done with it.

To deserialize the response as JSON:

ResponseDTO content = await response.ReadJson<ResponseDTO>();

To deserialize the response as XML:

ResponseDTO content = await response.ReadXml<ResponseDTO>();

To read the body as a byte array:

byte[] bytes = await response.ReadBytes();

Efficiently reading the response body

By default, the full response body is loaded in memory during the SendAsync call. This means that when calling the various Read* methods, the response body is already fully downloaded and is thereefore read from a memory stream.

To change this, you can set the HttpCompletionOption request option to HttpCompletionOption.ResponseHeadersRead (from System.Net.Http):

IHttpRequest request = new HttpRequest(url)
{
    Method = HttpMethod.Get,
    HttpCompletionOption = HttpCompletionOption.ResponseHeadersRead
};

Now, when you call methods such as ReadString or ReadJson, the response body will be streamed from the socket as it arrives.

The library will also take care of respecting the timeout you specified in the request, calculating how much time is left to read the response.

IHttpRequest request = new HttpRequest(url)
{
    Method = HttpMethod.Get,
    HttpCompletionOption = HttpCompletionOption.ResponseHeadersRead,
    Timeout = TimeSpan.FromSeconds(10)
};

// This call returns immediately after reading the response headers
IHttpResponse response = await request.SendAsync();

Console.WriteLine($"Reading the headers took {response.ElapsedMilliseconds} ms");

// This call will proceed with reading the HTTP response body from the socket
// and will throw HttpRequestTimeoutException if the response body is not
// fully read within 10 total seconds
string body = await response.ReadString();

Console.WriteLine($"Reading the headers+body took {response.ElapsedMilliseconds} ms in total");

The exception is if you use the ReadStream method: in that case PlainHttp cannot enforce a timeout when reading from that stream outside the library.

You also must take care of disposing the response manually when using ReadStream:

IHttpRequest request = new HttpRequest(url)
{
    Method = HttpMethod.Get,
    HttpCompletionOption = HttpCompletionOption.ResponseHeadersRead,
    Timeout = TimeSpan.FromSeconds(10)
};

// You MUST dispose the response manually
// when using HttpCompletionOption.ResponseHeadersRead and ReadStream()
using IHttpResponse response = await request.SendAsync();

Stream stream = await response.ReadStream();
// The timeout is not enforced if you read from `stream` here

In all other cases (any other Read* method), responses are always disposed automatically after reading the response body.

A note on XML deserialization: the ReadXml method uses XmlSerializer, which is not asynchronous. Therefore, the response body is unfortunately always fully read in memory (asynchronously) before deserializing it, no matter the HttpCompletionOption setting.

Downloading files

You can use the HttpCompletionOption.ResponseHeadersRead option to efficiently download files to disk:

IHttpRequest request = new HttpRequest(url)
{
    Method = HttpMethod.Get,
    HttpCompletionOption = HttpCompletionOption.ResponseHeadersRead
};

IHttpResponse response = await request.SendAsync();

await response.DownloadFileAsync("video.mp4");

Proxies

You can set a custom proxy per request:

IHttpRequest request = new HttpRequest(url)
{
    Proxy = new Uri("http://example.org:3128")
};

Proxy credentials are supported and are automatically parsed from the URI:

IHttpRequest request = new HttpRequest(url)
{
    Proxy = new Uri("http://user:pass@example.com:3128")
};

Note that due to the implementation of proxies in .NET, proxy credentials are only sent from the second request onwards and only if the proxy responded with 407 Proxy Authentication Required. See this issue for more details.

URL building

This library includes the Flurl URL builder as a dependency. Some Flurl-provided utilities are used internally but you can also use it to build URLs in an easier way (thanks Todd Menier!):

string url = "http://random.org"
    .SetQueryParam("locale", "it")
    .SetQueryParam("timestamp", DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());

Testing mode

Unit testing HTTP requests is easy with PlainHttp. You can enqueue HTTP responses that will be dequeued in sequence.

This mechanism is "async safe": the TestingMode property is static but wrapped in to an AsyncLocal instance, so that you can run your tests in parallel.

// Run this once
TestingMode http = new TestingMode();
HttpRequest.SetTestingMode(http);

// Then enqueue HTTP responses
HttpResponseMessage msg = new HttpResponseMessage()
{
    StatusCode = (HttpStatusCode)200,
    Content = new StringContent("oh hello")
};

http.RequestsQueue.Enqueue(msg);

// Then send your requests normally, in the same async context

Custom serialization

You can implement your own custom serializer by implementing the IPayload interface.

For example, here's how you can use Newtonsoft.Json instead of System.Text.Json:

public class NewtonsoftJsonPayload : IPayload
{
    private readonly object payload;
    private readonly JsonSerializerSettings? settings;
    
    public NewtonsoftJsonPayload(object payload)
    {
        this.payload = payload;
    }

    public NewtonsoftJsonPayload(object payload, JsonSerializerSettings settings) : this(payload)
    {
        this.settings = settings;
    }

    public HttpContent Serialize()
    {
        return new StringContent(
            content: JsonConvert.SerializeObject(payload, settings),
            encoding: Encoding.UTF8,
            mediaType: "application/json"
        );
    }
}

Then use it like this:

IHttpRequest request = new HttpRequest(url)
{
    Method = HttpMethod.Post,
    Payload = new NewtonsoftJsonPayload(new
    {
        something = "hello"
    })
};

Customizing HttpClient defaults

You can customize how HttpClients and the underlying SocketsHttpHandler are created by changing the static HttpClientFactory property.

The default factory provides some level of customization, which you can pass to the constructor. For example:

HttpRequest.HttpClientFactory = new HttpClientFactory(new HttpClientFactory.HttpHandlerOptions
{
    IgnoreCertificateValidationErrors = true
});

These options will apply to both proxied and non-proxied HttpClients. You can however choose different settings for proxied and non-proxied clients:

HttpRequest.HttpClientFactory = new HttpClientFactory(
    // Normal requests
    new HttpClientFactory.HttpHandlerOptions
    {
        IgnoreCertificateValidationErrors = true
    },
    // Proxied requests
    new HttpClientFactory.HttpHandlerOptions
    {
        IgnoreCertificateValidationErrors = false
    }
);

These are all the available options with their defaults:

public record HttpHandlerOptions
{
    public TimeSpan PooledConnectionLifetime { get; init; } = TimeSpan.FromMinutes(10);
    public TimeSpan PooledConnectionIdleTimeout { get; init; } = TimeSpan.FromMinutes(1);
    public TimeSpan ConnectTimeout { get; init; } = Timeout.InfiniteTimeSpan;
    public DecompressionMethods AutomaticDecompression { get; init; } = DecompressionMethods.All;
    public SslProtocols EnabledSslProtocols { get; init; } = SslProtocols.None;
    public bool IgnoreCertificateValidationErrors { get; init; }
}

The meanings of these options (which usually map to SocketsHttpHandler properties) are the following:

  • PooledConnectionLifetime: the maximum lifetime of a connection in the pool.
  • PooledConnectionIdleTimeout: the maximum idle time of a connection in the pool. If a connection is idle for more than this time, it will be closed.
  • ConnectTimeout: the timeout for establishing a connection to the server.
  • AutomaticDecompression: the decompression methods to use for the response body. By default, all methods (gzip, DEFLATE and Brotli) are enabled.
  • EnabledSslProtocols: the SSL/TLS protocols to use. By default, the system default is used.
  • IgnoreCertificateValidationErrors: whether to ignore certificate validation errors.

Note that when applied to proxied clients these options will apply to the connection to the proxy server itself.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.0 84 12/19/2024
2.0.0 3,458 1/24/2024
2.0.0-pre3 99 1/24/2024
2.0.0-pre2 336 11/21/2023
2.0.0-pre1 246 10/28/2023
1.3.0 1,574 8/1/2022
1.2.0 1,360 11/27/2020
1.1.2 793 6/24/2020
1.1.1 444 6/19/2020
1.1.0 452 6/18/2020
1.0.3 930 12/12/2019
1.0.2 547 12/4/2019
1.0.1 787 10/3/2019
1.0.0 940 8/16/2019

Release notes are published on GitHub: https://github.com/matteocontrini/PlainHttp/releases