Rabbit.Common.Http
1.3.1
dotnet add package Rabbit.Common.Http --version 1.3.1
NuGet\Install-Package Rabbit.Common.Http -Version 1.3.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Rabbit.Common.Http" Version="1.3.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Rabbit.Common.Http" Version="1.3.1" />
<PackageReference Include="Rabbit.Common.Http" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Rabbit.Common.Http --version 1.3.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Rabbit.Common.Http, 1.3.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Rabbit.Common.Http@1.3.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Rabbit.Common.Http&version=1.3.1
#tool nuget:?package=Rabbit.Common.Http&version=1.3.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Rabbit.Common.Http
Typed HTTP client wrapper with JSON serialization. Supports GET/POST with automatic deserialization, custom headers, timeout configuration, and multiple encodings.
HTTP 客户端组件,封装常用 HTTP 请求操作。
安装
dotnet add package Rabbit.Common.Http
依赖
- Rabbit.Common.Lite
- Microsoft.Extensions.Http 10.0.8
- Microsoft.Extensions.Http.Resilience 10.6.0
功能
- GET/POST 请求封装
- 支持多种编码(UTF-8、GBK 等)
- JSON/FormData 自动序列化
- 自定义请求头
- 可选的重试 + 熔断策略(默认关闭)
使用方式
Console 项目
using Microsoft.Extensions.DependencyInjection;
using Rabbit.Common.Http;
// 1. 创建 DI 容器
var services = new ServiceCollection();
// 2. 注册 HttpClient
services.AddHttpClientHelperConfigure(
timeoutSeconds: 30,
configureHttpClient: client =>
{
client.DefaultRequestHeaders.Add("X-Api-Key", "your-key");
}
);
// 3. 构建服务提供者
var provider = services.BuildServiceProvider();
// 4. 发送请求
var http = provider.GetRequiredService<HttpClientHelper>();
// GET 请求
var result = await http.GetAsync("https://api.example.com/data");
// GET 并反序列化
var user = await http.GetAsync<User>("https://api.example.com/users/1");
// POST JSON
var response = await http.PostJsonAsync(
"https://api.example.com/users",
new { Name = "Test", Age = 25 }
);
Web API 项目
using Rabbit.Common.Http;
services.AddHttpClientHelperConfigure(
timeoutSeconds: 30,
configureHttpClient: client =>
{
client.DefaultRequestHeaders.Add("X-Api-Key", "your-key");
}
);
开启重试策略
services.AddHttpClientHelperConfigure(
timeoutSeconds: 30,
configureResilience: options =>
{
options.EnableRetry = true;
options.MaxRetryAttempts = 3; // 最大重试次数,默认 3
options.RetryDelay = TimeSpan.FromSeconds(1); // 重试间隔,默认 1s
options.RetryBackoffType = RetryBackoffType.Exponential; // 退避策略
}
);
开启熔断策略
services.AddHttpClientHelperConfigure(
timeoutSeconds: 30,
configureResilience: options =>
{
options.EnableCircuitBreaker = true;
options.FailureRatio = 0.5; // 失败率 50% 触发熔断
options.SamplingDuration = TimeSpan.FromSeconds(30); // 采样窗口
options.BreakDuration = TimeSpan.FromSeconds(30); // 熔断持续时间
options.MinimumThroughput = 10; // 最小请求数
}
);
同时开启重试 + 熔断
services.AddHttpClientHelperConfigure(
timeoutSeconds: 30,
configureResilience: options =>
{
options.EnableRetry = true;
options.EnableCircuitBreaker = true;
}
);
使用示例
public class ThirdPartyService
{
private readonly HttpClientHelper _http;
public ThirdPartyService(HttpClientHelper http)
{
_http = http;
}
// GET 请求
public async Task<string> GetData(string url)
{
return await _http.GetAsync(url);
}
// GET 并反序列化
public async Task<WeatherData?> GetWeather(string city)
{
var url = $"https://api.weather.com/v1/current?city={city}";
return await _http.GetAsync<WeatherData>(url);
}
// POST JSON
public async Task<string> PostOrder(Order order)
{
return await _http.PostJsonAsync(
"https://api.example.com/orders",
order
);
}
// POST FormData
public async Task<string> UploadForm(Dictionary<string, string> data)
{
return await _http.PostFormDataAsync(
"https://api.example.com/form",
data
);
}
}
使用 GBK 编码
// 某些老系统使用 GBK 编码
var content = await _http.GetAsync(
url,
encoding: Encoding.GetEncoding("GBK")
);
JSON 时间转换器
// 处理 yyyy-MM-dd HH:mm:ss 格式
var options = new JsonSerializerOptions
{
Converters = { new JsonDateTimeConverter() }
};
API 参考
| 方法 | 说明 |
|---|---|
GetAsync |
GET 请求 |
GetAsync<T> |
GET 并反序列化 |
PostJsonAsync |
POST JSON |
PostJsonAsync<T> |
POST JSON 并反序列化 |
PostFormDataAsync |
POST 表单 |
PostStringAsync |
POST 字符串 |
许可证
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- Microsoft.Extensions.Http (>= 10.0.8)
- Microsoft.Extensions.Http.Resilience (>= 10.6.0)
- Rabbit.Common.Lite (>= 1.3.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Rabbit.Common.Http:
| Package | Downloads |
|---|---|
|
Rabbit.Common.Full
Meta-package referencing all Rabbit.Common utility packages. / Rabbit.Common 全家桶,引用所有子包 |
GitHub repositories
This package is not used by any popular GitHub repositories.