HttpClientLite.Extensions
1.0.0
dotnet add package HttpClientLite.Extensions --version 1.0.0
NuGet\Install-Package HttpClientLite.Extensions -Version 1.0.0
<PackageReference Include="HttpClientLite.Extensions" Version="1.0.0" />
paket add HttpClientLite.Extensions --version 1.0.0
#r "nuget: HttpClientLite.Extensions, 1.0.0"
// Install HttpClientLite.Extensions as a Cake Addin #addin nuget:?package=HttpClientLite.Extensions&version=1.0.0 // Install HttpClientLite.Extensions as a Cake Tool #tool nuget:?package=HttpClientLite.Extensions&version=1.0.0
HttpClientLite
Description
HttpClientLite is a.NET-based, highly scalable HTTP client management library that provides comprehensive management of HTTP requests, including:
- Request/response configuration
- Exception handling
- Supports synchronous and asynchronous operations
- Rich extension methods, support GET, POST, PUT and other operations
- Supports FormData and JSON data transmission
Feature
- Highly modular : Easily expand functionality through configuration and processor.
- Request cloning : Support for 'HttpRequestMessage' cloning, easy to retry or log processing.
- Extended Method : Provides a simplified method of common HTTP requests.
- Exception Management : Supports custom exception handling logic.
Software Architecture
Supports net4.6.2, net4.7.2, net4.8.1, net6, net8, and net9
Installation
dotnet add package HttpClientLite.Extensions
Usage
using HttpClientLite;
using HttpClientLite.Extensions;
using HttpClientLite.Handlers.interfaces;
using HttpClientLite.Configurations.interfaces;
var httpManager = new HttpManager("https://api.example.com");
Send GET request
var response = await httpManager.GetAsync("/endpoint");
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
Sending POST requests (JSON data)
var requestBody = new { key = "value" };
var response = await httpManager.PostJsonAsync("/endpoint", requestBody);
Add exception handling
httpManager.HttpManagerExceptionHandler((exception, response) =>
{
Console.WriteLine($"Error: {exception.Message}");
});
Configuration and extension
Configuration HttpManager
httpManager.HttpManagerConfiguration(manager =>
{
manager.BaseAddress = "https://api.customdomain.com";
});
or Custom classes inherit and implement HttpClientLite.Configurations.interfaces.IHttpManagerConfiguration
httpManager.Configuration(new CustomHttpManagerConfiguration());
Configuration HttpClient
httpManager.HttpClientConfiguration(client =>
{
client.Timeout = TimeSpan.FromSeconds(30);
client.DefaultRequestHeaders.Add("Authorization", "Bearer token");
});
or Custom classes inherit and implement HttpClientLite.Configurations.interfaces.IHttpClientConfiguration
httpManager.Configuration(new CustomHttpClientConfiguration());
Configuration HttpRequestMessage
httpManager.RequestMessageConfiguration(request =>
{
request.Headers.Add("Custom-Header", "CustomValue");
});
or Custom classes inherit and implement HttpClientLite.Configurations.interfaces.IRequestMessageConfiguration
httpManager.Configuration(new CustomRequestMessageConfiguration());
Customize response handling
httpManager.ResponseMessageHandler((manager, client, request, response, token) =>
{
if (!response.IsSuccessStatusCode)
{
Console.WriteLine($"Request failed with status code {response.StatusCode}");
}
return response;
});
or Custom classes inherit and implement HttpClientLite.Handlers.interfaces.IResponseMessageHandler
httpManager.Handler(new CustomResponseMessageHandler());
Send file
var content = new MultipartFormDataContent();
content.AddFile("file", "example.txt", File.ReadAllBytes("example.txt"));
var request = new HttpRequestMessage(HttpMethod.Post, "/upload") { Content = content };
var response = await httpManager.SendAsync(request);
or
Stream stream = File.OpenRead("example.txt");
content.AddFile("file", "example.txt", stream);
List of extension methods
The current implementation is more commonly used:
HttpClientLite.Extensions.HttpManagerExtensions
Method | Description |
---|---|
GetAsync | Send GET requests asynchronously |
PostJsonAsync | Send the POST JSON request asynchronously |
PostFormDataAsync | The POST FormData request is sent asynchronously |
PutJsonAsync | Asynchronously send a PUT JSON request |
PutFormDataAsync | Send the PUT FormData request asynchronously |
Example
Comprehensive example
using System.Web;
using System.Net.Http;
using HttpClientLite;
using HttpClientLite.Extensions;
using HttpClientLite.Handlers.interfaces;
using HttpClientLite.Configurations.interfaces;
var httpManager = new HttpManager("https://api.example.com");
// Configuration HttpClient
httpManager.HttpClientConfiguration(client =>
{
client.DefaultRequestHeaders.Add("Authorization", "Bearer example-token");
});
// Configuration HttpRequestMessage
httpManager.RequestMessageConfiguration(request =>
{
request.Headers.Add("Custom-Header", "HeaderValue");
});
// Add exception handling
httpManager.HttpManagerExceptionHandler((exception, response) =>
{
Console.WriteLine($"Request failed: {exception.Message}");
});
// Send request
var response = await httpManager.GetAsync("/users");
string content = await response.Content.ReadAsStringAsync();
Console.WriteLine(content);
Contribution
- Fork the repository
- Create Feat_xxx branch
- Commit your code
- Create Pull Request
Product | Versions 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 is compatible. 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 is compatible. |
.NET Framework | net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 is compatible. net48 was computed. net481 is compatible. |
-
.NETFramework 4.6.2
- HttpClientLite (>= 1.0.0)
- Newtonsoft.Json (>= 13.0.3)
-
.NETFramework 4.7.2
- HttpClientLite (>= 1.0.0)
- Newtonsoft.Json (>= 13.0.3)
-
.NETFramework 4.8.1
- HttpClientLite (>= 1.0.0)
- Newtonsoft.Json (>= 13.0.3)
-
net6.0
- HttpClientLite (>= 1.0.0)
- Newtonsoft.Json (>= 13.0.3)
-
net8.0
- HttpClientLite (>= 1.0.0)
- Newtonsoft.Json (>= 13.0.3)
-
net9.0
- HttpClientLite (>= 1.0.0)
- Newtonsoft.Json (>= 13.0.3)
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 |
---|---|---|
1.0.0 | 92 | 12/7/2024 |