Rabbit.Common.Http
1.2.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Rabbit.Common.Http --version 1.2.0
NuGet\Install-Package Rabbit.Common.Http -Version 1.2.0
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.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Rabbit.Common.Http" Version="1.2.0" />
<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.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Rabbit.Common.Http, 1.2.0"
#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.2.0
#: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.2.0
#tool nuget:?package=Rabbit.Common.Http&version=1.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Rabbit.Common.Http
HTTP 客户端组件,封装常用 HTTP 请求操作。
安装
dotnet add package Rabbit.Common.Http
依赖
- Rabbit.Common.Lite
- Microsoft.Extensions.Http 10.0.4
功能
- 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");
}
);
使用示例
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.4)
- Rabbit.Common.Lite (>= 1.2.0)
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.