Sage.Http 1.0.0

dotnet add package Sage.Http --version 1.0.0
                    
NuGet\Install-Package Sage.Http -Version 1.0.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="Sage.Http" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sage.Http" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Sage.Http" />
                    
Project file
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 Sage.Http --version 1.0.0
                    
#r "nuget: Sage.Http, 1.0.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 Sage.Http@1.0.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=Sage.Http&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Sage.Http&version=1.0.0
                    
Install as a Cake Tool

Sage.Http

简介

Sage.Http 是一个 HTTP 客户端封装库,简化了 HTTP 请求的发送和处理。支持HTTP工厂模式,支持单例模式,支持更多的简化API,支持链式调用等。

主要特性

  • 支持多种认证方式(API Key、Basic、Bearer Token、自定义)
  • 请求构建器模式
  • 支持重试、超时、熔断等高级功能
  • 支持同步和异步操作
  • 支持 JSON 序列化/反序列化
  • 完全兼容 AOT 编译

使用示例

基本使用
using Sage.Http.Core;
using Sage.Http.Authentication;
using Microsoft.Extensions.Logging;
using System.Net.Http;

// 创建 HttpRequestManager
var httpClient = new HttpClient();
var logger = LoggerFactory.Create(builder => builder.
AddConsole()).CreateLogger<HttpRequestManager>();
var requestManager = new HttpRequestManager
(httpClient, logger);

// 设置认证
requestManager.SetDefaultAuthProvider(new 
BearerTokenAuthProvider("your-token"));

// 发送 GET 请求
var response = await requestManager.GetAsync("https://
api.example.com/data");
if (response.IsSuccessStatusCode)
{
    var content = await response.Content.
    ReadAsStringAsync();
    Console.WriteLine(content);
}

// 发送 POST 请求并反序列化 JSON 响应
var data = new { Name = "Test", Value = 123 };
var result = await requestManager.
PostJsonAsync<ResponseType>("https://api.example.com/
data", data);
Console.WriteLine($"Result: {result.Status}");

重试和熔断配置的DI
// 读取HTTP客户端配置
var httpClientSettings = builder.Configuration.GetSection("HttpClients").Get<HttpClientSettings>()
      ?? throw new InvalidOperationException("未找到HTTP客户端配置,请检查appsettings.json文件");

// 1. 先注册HTTP客户端工厂配置(再http客户端功能)
  builder.Services.AddHttpRequestManagerFactory();

// 2. 再注册Http客户端及配置服务(包括基础配置,重试、熔断)
  builder.Services.AddHttpClientWithResilience(
      name: "ApiResourceServicesClient",
      configureClient: client =>
      {
          // 不使用BaseUrl
          //client.BaseAddress = new Uri(httpClientSettings.ApiResourceServicesClient.BaseUrl);
          client.Timeout = TimeSpan.FromSeconds(httpClientSettings.ApiResourceServicesClient.TimeoutSeconds);
          client.DefaultRequestHeaders.Add("Accept", "application/json");
          // 开启http3的支持
          client.DefaultRequestVersion = new Version(3, 0);
          // 如果不支持自动降级
          client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower;
      },
      retryOptions: httpClientSettings.ApiResourceServicesClient.Retry, // 重试配置
      circuitBreakerOptions: httpClientSettings.ApiResourceServicesClient.CircuitBreaker // 熔断配置
    );

许可证

版权所有 © 2025 甲壳虫科技 团队。

贡献

欢迎提交问题和功能请求。 QQ Group: 1054304346

作者

甲壳虫科技

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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.  net10.0 was computed.  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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 110 7/16/2025