GHttpHelper 2.0.2
dotnet add package GHttpHelper --version 2.0.2
NuGet\Install-Package GHttpHelper -Version 2.0.2
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="GHttpHelper" Version="2.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GHttpHelper --version 2.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: GHttpHelper, 2.0.2"
#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.
// Install GHttpHelper as a Cake Addin #addin nuget:?package=GHttpHelper&version=2.0.2 // Install GHttpHelper as a Cake Tool #tool nuget:?package=GHttpHelper&version=2.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
GHttpHelper
一个基于 .NET 的 HTTP 请求库,提供类似 Axios 的 API 和功能。
特性
- 支持常用的 HTTP 方法(GET、POST、PUT、DELETE、PATCH)
- 请求/响应拦截器
- 自动重试机制
- 请求并发控制
- GET 请求缓存
- 支持 Form 和 JSON 格式数据
- 可配置的请求超时
- 支持取消请求
- 数据转换器
安装
dotnet add package GHttpHelper
基础使用
创建实例
var axios = new GAxios(new AxiosConfig
{
BaseUrl = "https://api.example.com",
Timeout = 5000
});
GET 请求
// 基础 GET 请求
var result = await axios.Get<UserInfo>("/users/1");
// 带请求头的 GET 请求
var headers = new WebHeaderCollection();
headers.Add("Authorization", "Bearer token");
var result = await axios.Get<UserInfo>("/users/1", headers);
POST 请求
// Form 格式提交
var formResult = await axios.Post<ResponseData>("/api/submit", new {
name = "test",
age = 18
});
// JSON 格式提交
var jsonResult = await axios.Post<ResponseData>("/api/submit", new {
name = "test",
age = 18
}, RequestType.Json);
PUT 和 DELETE 请求
// PUT 请求
var putResult = await axios.Put<ResponseData>("/api/users/1", new {
name = "updated"
});
// DELETE 请求
var deleteResult = await axios.Delete<ResponseData>("/api/users/1");
高级特性
拦截器
// 添加请求拦截器
axios.RequestInterceptors.Use(config =>
{
config.Header.Add("Authorization", "Bearer token");
Console.WriteLine("请求发送前");
});
// 添加响应拦截器
axios.ResponseInterceptors.Use(
response =>
{
Console.WriteLine("响应接收后");
},
error =>
{
Console.WriteLine($"请求错误:{error.Message}");
}
);
重试机制
axios.RetryConfig = new RetryConfig
{
RetryCount = 3,
RetryDelay = 1000,
RetryCondition = ex => ex is HttpRequestException
};
并发控制
// 设置最大并发请求数
axios.MaxConcurrentRequests = 5;
缓存控制
// 设置 GET 请求缓存时间
axios.CacheDuration = TimeSpan.FromMinutes(10);
创建新实例
var newAxios = axios.Create(new AxiosConfig
{
BaseUrl = "https://api2.example.com",
Timeout = 5000
});
数据转换
// 请求数据转换
axios.TransformRequest = data =>
{
return JsonConvert.SerializeObject(data, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
};
// 响应数据转换
axios.TransformResponse = (data, type) =>
{
return JsonConvert.DeserializeObject(data, type, new JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Local
});
};
统一请求配置
var result = await axios.Request<ResponseData>(new AxiosRequestConfig
{
Url = "/api/data",
Method = "POST",
Data = new { id = 1 },
RequestType = RequestType.Json,
Headers = new WebHeaderCollection()
});
错误处理
try
{
var result = await axios.Get<UserInfo>("/users/1");
}
catch (Exception ex)
{
Console.WriteLine($"请求失败:{ex.Message}");
}
注意事项
- GET 请求的缓存仅在内存中保存
- 重试机制默认对所有异常生效,可通过 RetryCondition 自定义重试条件
- 并发控制会对所有请求生效,包括重试的请求
- 拦截器按添加顺序执行
许可证
MIT License
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.ICU.ICU4C.Runtime (>= 72.1.0.3)
- WandhiHelper (>= 1.0.2)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on GHttpHelper:
Package | Downloads |
---|---|
OPQ.SDK
OPQ Netcore开发框架 |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.0.2 | 141 | 12/23/2024 |
2.0.1 | 78 | 12/23/2024 |
2.0.0 | 90 | 12/22/2024 |
1.0.17 | 268 | 4/10/2023 |
1.0.16 | 862 | 4/22/2022 |
1.0.15 | 457 | 10/29/2020 |
1.0.14 | 532 | 8/14/2020 |
1.0.13 | 522 | 8/14/2020 |
1.0.12 | 485 | 8/14/2020 |
1.0.11 | 474 | 8/14/2020 |
1.0.10 | 581 | 8/13/2020 |
1.0.9 | 490 | 8/13/2020 |
1.0.8 | 512 | 8/13/2020 |
1.0.7 | 489 | 7/10/2020 |
1.0.6 | 533 | 12/27/2019 |
1.0.5 | 513 | 12/27/2019 |
1.0.4 | 526 | 12/27/2019 |
1.0.3 | 529 | 11/25/2019 |
1.0.2 | 542 | 11/25/2019 |
1.0.0 | 499 | 11/25/2019 |
破坏性更新