Universal.Common.Net.Http
5.1.1
dotnet add package Universal.Common.Net.Http --version 5.1.1
NuGet\Install-Package Universal.Common.Net.Http -Version 5.1.1
<PackageReference Include="Universal.Common.Net.Http" Version="5.1.1" />
<PackageVersion Include="Universal.Common.Net.Http" Version="5.1.1" />
<PackageReference Include="Universal.Common.Net.Http" />
paket add Universal.Common.Net.Http --version 5.1.1
#r "nuget: Universal.Common.Net.Http, 5.1.1"
#:package Universal.Common.Net.Http@5.1.1
#addin nuget:?package=Universal.Common.Net.Http&version=5.1.1
#tool nuget:?package=Universal.Common.Net.Http&version=5.1.1
Universal.Common.Net.Http
Class library accelerate building clients for various web services.
Basic Usage
You can derive a strongly-typed client from HttpServiceClient or the more specialized versions such as JsonHttpServiceClient.
public class MyClient : HttpServiceClient
{
// ...
}
Alternative, you can also use these classes directly.
InterceptingDelegatingHandler
The InterceptingDelegatingHandler is a powerful tool for intercepting and modifying HTTP requests and responses. It allows you to define rules that match specific requests and either replace them with custom responses or modify the actual responses.
Basic Setup
var handler = new InterceptingDelegatingHandler();
var httpClient = new HttpClient(handler);
Intercepting Requests with Custom Responses
Use When() to define matching conditions and RespondWith() to provide custom responses:
var handler = new InterceptingDelegatingHandler()
.When(req => req.RequestUri.AbsolutePath.Contains("/api/test"))
.RespondWith(req => new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("Mocked response")
});
Modifying Real Responses
Use Modify() to transform responses from actual HTTP calls:
var handler = new InterceptingDelegatingHandler()
.When(req => req.Method == HttpMethod.Get)
.Modify((req, resp) =>
{
resp.Headers.Add("X-Custom-Header", "Modified");
return resp;
});
Async Handlers
Both RespondWith() and Modify() support async operations:
var handler = new InterceptingDelegatingHandler()
.When(req => req.RequestUri.Host == "api.example.com")
.RespondWith(async req =>
{
var data = await GetDataAsync();
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(data)
};
});
Multiple Rules
Chain multiple rules to handle different scenarios:
var handler = new InterceptingDelegatingHandler()
.When(req => req.RequestUri.AbsolutePath == "/api/users")
.RespondWith(req => new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("{\"users\":[]}")
})
.When(req => req.RequestUri.AbsolutePath == "/api/products")
.RespondWith(req => new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("{\"products\":[]}")
});
Common Use Cases
Testing and Mocking
// Mock external API calls in tests
var handler = new InterceptingDelegatingHandler()
.When(req => req.RequestUri.Host == "external-api.com")
.RespondWith(req => new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("{\"status\":\"success\"}")
});
Adding Authentication Headers
// Automatically add authentication to responses
var handler = new InterceptingDelegatingHandler()
.When(req => req.RequestUri.AbsolutePath.StartsWith("/api/"))
.Modify(async (req, resp) =>
{
var token = await GetAuthTokenAsync();
resp.Headers.Add("Authorization", $"Bearer {token}");
return resp;
});
Error Simulation
// Simulate errors for testing
var handler = new InterceptingDelegatingHandler()
.When(req => req.Headers.Contains("X-Simulate-Error"))
.RespondWith(req => new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent("Simulated error")
});
Using with HttpServiceClient
public class MyClient : HttpServiceClient
{
protected override HttpClient CreateHttpClient()
{
var handler = new InterceptingDelegatingHandler()
.When(req => req.RequestUri.AbsolutePath.Contains("/slow"))
.Modify((req, resp) =>
{
resp.Headers.Add("X-Cache", "HIT");
return resp;
});
return new HttpClient(handler);
}
}
Convenience Extension Methods
The Universal.Common.Net.Http.Extensions namespace provides extension methods that simplify common matching scenarios:
WhenUri
Match requests by URI pattern (case-insensitive):
var handler = new InterceptingDelegatingHandler()
.WhenUri("/api/users")
.RespondWith(req => new HttpResponseMessage(HttpStatusCode.OK));
WhenUriMatches
Match requests using regular expressions:
var handler = new InterceptingDelegatingHandler()
.WhenUriMatches(new Regex(@"/api/users/\d+"))
.RespondWith(req => new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("{\"id\":123,\"name\":\"John\"}")
});
WhenMethod
Match requests by HTTP method:
var handler = new InterceptingDelegatingHandler()
.WhenMethod(HttpMethod.Post)
.Modify((req, resp) =>
{
resp.Headers.Add("X-Request-Method", "POST");
return resp;
});
WhenHeader
Match requests containing a specific header with a given value:
var handler = new InterceptingDelegatingHandler()
.WhenHeader("X-API-Version", "v2")
.RespondWith(req => new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("{\"version\":\"2.0\"}")
});
Combining Extension Methods
Chain multiple conditions for more complex scenarios:
var handler = new InterceptingDelegatingHandler()
.WhenMethod(HttpMethod.Get)
.WhenUri("/api/")
.WhenHeader("Authorization", "Bearer test-token")
.RespondWith(req => new HttpResponseMessage(HttpStatusCode.OK));
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Universal.Common (>= 8.3.4)
NuGet packages (28)
Showing the top 5 NuGet packages that depend on Universal.Common.Net.Http:
| Package | Downloads |
|---|---|
|
RedMarble.Pronto.Scope
A client library for Pronto and Scope APIs. |
|
|
Universal.Common.Net.WebSocket
Class library that implements the WebSocket protocol according to the standards in RFC6455 using a simple API. |
|
|
RedMarble.ExTrack.Client
Class library to interact with the ExTrack system. |
|
|
Universal.AustralianBusinessRegister
Class library to query the Australian Business Register web services. |
|
|
Universal.Ckan.Client
Basic client for interacting with the CKAN v3 APIs. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 5.1.1 | 604 | 11/21/2025 |
| 5.1.0 | 595 | 7/28/2025 |
| 5.0.0 | 8,610 | 11/4/2022 |
| 4.0.0 | 6,641 | 10/13/2021 |
| 3.3.0 | 1,680 | 9/29/2021 |
| 3.2.1 | 2,879 | 8/17/2021 |
| 3.2.0 | 1,413 | 8/17/2021 |
| 3.1.1 | 27,733 | 3/31/2021 |
| 3.1.0 | 16,628 | 2/5/2021 |
| 3.0.0 | 3,335 | 11/30/2020 |
| 2.2.0 | 1,605 | 11/30/2020 |
| 2.1.0.1 | 4,385 | 10/19/2020 |
| 2.1.0 | 2,472 | 9/30/2020 |
| 2.0.0 | 3,983 | 8/24/2020 |
| 1.1.0 | 1,894 | 8/18/2020 |
| 1.0.1 | 20,753 | 2/26/2020 |
| 1.0.0 | 2,369 | 2/13/2020 |
| 0.6.0 | 29,542 | 7/5/2019 |
| 0.5.1 | 4,036 | 7/2/2019 |
| 0.5.0 | 1,837 | 7/2/2019 |
| 0.4.9 | 5,246 | 4/5/2019 |
| 0.4.8.1 | 6,023 | 2/12/2019 |
| 0.4.8 | 6,999 | 1/16/2019 |
| 0.4.7 | 2,266 | 1/8/2019 |
| 0.4.6 | 3,291 | 10/27/2018 |
| 0.4.5 | 3,683 | 10/12/2018 |
| 0.4.4 | 3,865 | 10/4/2018 |
| 0.4.3 | 1,960 | 10/4/2018 |
| 0.4.2.1 | 1,946 | 10/4/2018 |
| 0.4.2 | 2,011 | 9/28/2018 |
| 0.4.1 | 1,981 | 9/26/2018 |
| 0.4.0 | 4,485 | 9/10/2018 |
| 0.3.0 | 3,865 | 9/6/2018 |
New InterceptingDelegatingHandler to provide HTTP request interception.