HttpClientLite is a.NET-based, highly scalable HTTP client management library that provides comprehensive management of HTTP requests, including:
Supports net4.6.2, net4.7.2, net4.8.1, net6, net8, and net9
dotnet add package HttpClientLite.Extensions
using HttpClientLite;
using HttpClientLite.Extensions;
using HttpClientLite.Handlers.interfaces;
using HttpClientLite.Configurations.interfaces;
var httpManager = new HttpManager("https://api.example.com");
var response = await httpManager.GetAsync("/endpoint");
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
var requestBody = new { key = "value" };
var response = await httpManager.PostJsonAsync("/endpoint", requestBody);
httpManager.HttpManagerExceptionHandler((exception, response) =>
{
Console.WriteLine($"Error: {exception.Message}");
});
httpManager.HttpManagerConfiguration(manager =>
{
manager.BaseAddress = "https://api.customdomain.com";
});
or Custom classes inherit and implement HttpClientLite.Configurations.interfaces.IHttpManagerConfiguration
httpManager.Configuration(new CustomHttpManagerConfiguration());
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());
httpManager.RequestMessageConfiguration(request =>
{
request.Headers.Add("Custom-Header", "CustomValue");
});
or Custom classes inherit and implement HttpClientLite.Configurations.interfaces.IRequestMessageConfiguration
httpManager.Configuration(new CustomRequestMessageConfiguration());
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());
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);
The current implementation is more commonly used:
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 |
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);
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。