# HttpClientLite **Repository Path**: Ausername/HttpClientLite ## Basic Information - **Project Name**: HttpClientLite - **Description**: HttpClientLite 是一个基于 .NET 的高可扩展 HTTP 客户端管理库,提供了对 HTTP 请求的全面管理,包括: 请求/响应配置 异常处理 支持同步与异步操作 丰富的扩展方法,支持 GET、POST、PUT 等操作 支持 FormData 和 JSON 数据传输 - **Primary Language**: C# - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 0 - **Created**: 2024-12-07 - **Last Updated**: 2026-05-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: Http, HttpClient, HttpManager, dotNET, post ## README # 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 1. **Highly modular** : Easily expand functionality through configuration and processor. 2. **Request cloning** : Support for 'HttpRequestMessage' cloning, easy to retry or log processing. 3. **Extended Method** : Provides a simplified method of common HTTP requests. 4. **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 ~~~csharp using HttpClientLite; using HttpClientLite.Extensions; using HttpClientLite.Handlers.interfaces; using HttpClientLite.Configurations.interfaces; var httpManager = new HttpManager("https://api.example.com"); ~~~ #### Send GET request ~~~csharp var response = await httpManager.GetAsync("/endpoint"); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); ~~~ #### Sending POST requests (JSON data) ~~~csharp var requestBody = new { key = "value" }; var response = await httpManager.PostJsonAsync("/endpoint", requestBody); ~~~ #### Add exception handling ~~~csharp httpManager.HttpManagerExceptionHandler((exception, response) => { Console.WriteLine($"Error: {exception.Message}"); }); ~~~ ### Configuration and extension #### Configuration HttpManager ~~~csharp httpManager.HttpManagerConfiguration(manager => { manager.BaseAddress = "https://api.customdomain.com"; }); ~~~ or Custom classes inherit and implement **HttpClientLite.Configurations.interfaces.IHttpManagerConfiguration** ~~~csharp httpManager.Configuration(new CustomHttpManagerConfiguration()); ~~~ #### Configuration HttpClient ~~~csharp httpManager.HttpClientConfiguration(client => { client.Timeout = TimeSpan.FromSeconds(30); client.DefaultRequestHeaders.Add("Authorization", "Bearer token"); }); ~~~ or Custom classes inherit and implement **HttpClientLite.Configurations.interfaces.IHttpClientConfiguration** ~~~csharp httpManager.Configuration(new CustomHttpClientConfiguration()); ~~~ #### Configuration HttpRequestMessage ~~~csharp httpManager.RequestMessageConfiguration(request => { request.Headers.Add("Custom-Header", "CustomValue"); }); ~~~ or Custom classes inherit and implement **HttpClientLite.Configurations.interfaces.IRequestMessageConfiguration** ~~~csharp httpManager.Configuration(new CustomRequestMessageConfiguration()); ~~~ #### Customize response handling ~~~csharp 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** ~~~csharp httpManager.Handler(new CustomResponseMessageHandler()); ~~~ ### Send file ~~~csharp 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 ~~~csharp 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 ~~~csharp 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 1. Fork the repository 2. Create Feat_xxx branch 3. Commit your code 4. Create Pull Request