代码拉取完成,页面将自动刷新
HttpClientLite 是一个基于 .NET 的高可扩展 HTTP 客户端管理库,提供了对 HTTP 请求的全面管理,包括:
HttpRequestMessage
的克隆,便于重试或日志处理。支持net4.6.2、net4.7.2、net4.8.1、net6、net8、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";
});
或者 自定义类继承并实现 HttpClientLite.Configurations.interfaces.IHttpManagerConfiguration
httpManager.Configuration(new CustomHttpManagerConfiguration());
httpManager.HttpClientConfiguration(client =>
{
client.Timeout = TimeSpan.FromSeconds(30);
client.DefaultRequestHeaders.Add("Authorization", "Bearer token");
});
或者 自定义类继承并实现 HttpClientLite.Configurations.interfaces.IHttpClientConfiguration
httpManager.Configuration(new CustomHttpClientConfiguration());
httpManager.RequestMessageConfiguration(request =>
{
request.Headers.Add("Custom-Header", "CustomValue");
});
或者 自定义类继承并实现 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;
});
或者 自定义类继承并实现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);
或者
Stream stream = File.OpenRead("example.txt");
content.AddFile("file", "example.txt", stream);
目前实现比较常用的:
方法 | 描述 |
---|---|
GetAsync | 异步发送 GET 请求 |
PostJsonAsync | 异步发送 POST JSON 请求 |
PostFormDataAsync | 异步发送 POST FormData 请求 |
PutJsonAsync | 异步发送 PUT JSON 请求 |
PutFormDataAsync | 异步发送 PUT FormData 请求 |
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");
// 配置 HttpClient
httpManager.HttpClientConfiguration(client =>
{
client.DefaultRequestHeaders.Add("Authorization", "Bearer example-token");
});
// 配置 HttpRequestMessage
httpManager.RequestMessageConfiguration(request =>
{
request.Headers.Add("Custom-Header", "HeaderValue");
});
// 添加异常处理
httpManager.HttpManagerExceptionHandler((exception, response) =>
{
Console.WriteLine($"Request failed: {exception.Message}");
});
// 发起请求
var response = await httpManager.GetAsync("/users");
string content = await response.Content.ReadAsStringAsync();
Console.WriteLine(content);
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。