Common.HttpClients 1.3.1-beta2

This is a prerelease version of Common.HttpClients.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Common.HttpClients --version 1.3.1-beta2
                    
NuGet\Install-Package Common.HttpClients -Version 1.3.1-beta2
                    
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="Common.HttpClients" Version="1.3.1-beta2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Common.HttpClients" Version="1.3.1-beta2" />
                    
Directory.Packages.props
<PackageReference Include="Common.HttpClients" />
                    
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 Common.HttpClients --version 1.3.1-beta2
                    
#r "nuget: Common.HttpClients, 1.3.1-beta2"
                    
#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 Common.HttpClients@1.3.1-beta2
                    
#: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=Common.HttpClients&version=1.3.1-beta2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Common.HttpClients&version=1.3.1-beta2&prerelease
                    
Install as a Cake Tool

Common.HttpClients

一个功能丰富的HTTP客户端库,支持日志记录、请求拦截、配置管理等特性。

主要特性

  • 🚀 高性能HTTP客户端
  • 📝 智能日志记录和审计
  • ⚙️ 灵活的配置管理
  • 🔒 请求/响应拦截
  • 📊 响应内容长度控制
  • 🎯 请求级别的日志控制

安装

dotnet add package Common.HttpClients

快速开始

1. 注册服务

// 使用默认配置
services.AddHttpClientServices();

// 或自定义配置
services.AddHttpClientServices(config =>
{
    config.AuditLog = true;
    config.FailThrowException = false;
    config.Timeout = 100;
    config.MaxOutputResponseLength = 1024 * 1024; // 1MB
});

2. 使用HTTP客户端

public class MyService
{
    private readonly IHttpHelper _httpHelper;

    public MyService(IHttpHelper httpHelper)
    {
        _httpHelper = httpHelper;
    }

    public async Task<string> GetDataAsync()
    {
        var result = await _httpHelper.GetAsync<string>(Host + "/get?q1=11&q2=22");

        return result;
    }
}

配置选项HttpConfig

属性 类型 默认值 说明
AuditLog bool true 是否启用审计日志(默认true)
FailThrowException bool false 失败时是否抛出异常(默认false)
Timeout int 100 超时时间(默认秒)
MaxOutputResponseLength int 1048576 最大输出响应长度(默认1MB)

请求

下面示例已经注入IHttpHelper

Get

var result = await _httpHelper.GetAsync<string>(Host + "/get?q1=11&q2=22");

还支持传递token以及传递请求头

Post

Json格式

支持传递字符串以及对象

var content = "{\"q\":\"123456\",\"a\":\"222\"}";
var result = await _httpHelper.PostAsync<string>(Host + "/post", content);
PostFormData
  • Task<T> PostFormDataAsync<T>(string url, MultipartFormDataContent formDataContent);
请求示例
using var form = new MultipartFormDataContent();

// bytes为文件字节数组
using var fileContent = new ByteArrayContent(bytes);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                                         {
                                             Name = "file", // 表单字段名称
                                             FileName = fileName // 文件名
                                         };
fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
form.Add(fileContent);

// 其他参数
using var content = new StringContent("其他参数值");
form.Add(content, "其他参数名称");

var requestUrl = $"{_difyApiBase}/v1/files/upload";
var response = await _httpHelper.PostFormDataAsync<FileUploadResponse>(requestUrl, form,
    new Dictionary<string, string> { { "Authorization", $"Bearer {_difyApiKey}" } });

日志

可以设置配置AuditLog来设置是否启用审计日志,默认为启用状态。

builder.Services.AddHttpClientService();

也可以为指定地址请求设置关闭审计日志,例如

var result = await _httpHelper.PostAsync<string>(Host + "/anything", list,
    headers: new Dictionary<string, string>() { { "X-Logger", "skip" } });

var result2 = await _httpHelper.PostAsync<string>(Host + "/anything", list,
    headers: new Dictionary<string, string>() { { "X-Skip-Logger", "" } });

可以通过在请求头设置X-Skip-Logger或者设置X-Logger值为none、skip进行跳过日志

版本更新记录

  • 1.3.1-beta2
    • 重试测试
    • 日志输出增加请求耗时
  • 1.3.1-beta1
    • 支持设置是否忽略不安全的SSL证书
  • 1.3.0-beta9
    • 更新响应日志输出内容
    • 增加支持CancellationToken
    • 移除对.NetStandard2.1支持
  • 1.3.0-beta8
    • 优化单独请求的日志输出
  • 1.3.0-beta7
    • 修复调用接口报错在忽略异常的情况下扔抛出错误
  • 1.3.0-beta6
    • 优化审计日志
  • 1.3.0-beta5
    • 增加全局设置超时时间以及针对指定请求设置超时时间
  • 1.3.0-beta4
    • 修改PostFormDataAsync方法,增加直接传递jwtToken入参
  • 1.3.0-beta3
    • 修复LoggingHandler被错误重用的问题,将其生命周期改为Transient
  • 1.3.0-beta2
    • 增加流式响应PostGetStreamAsync
    • 暴漏基础的SendAsync
  • 1.3.0-beta1
    • 支持.Net9
    • 增加请求审计日志
  • 1.2.3
    • 注入的时候支持设置是否异常直接抛出
  • 1.2.2
    • 增加x-www-form-urlencoded请求方式代码
    • 升级支持.Net8
  • 1.2.1
    • 增加get获取文件流的方法
  • 1.2.0
    • 升级支持.net7
  • 1.1.5
    • 修改put请求命名问题
    • 增加patch请求
  • 1.1.4
    • 处理多个构造函数的报错
    • 增加更加灵活的请求方式Send
  • 1.1.3
    • 增加http请求FormData形式去提交文件
    • 支持框架netstandard2.1、net6.0
  • 1.1.2
    • 更新post方法同时兼容string和其他类型
  • 1.1.1
    • 更新post方法,配置多个目标框架
  • 1.1.0
    • 更新框架版本为5.0
  • 1.0.0
    • 3.1版本的http请求公共库
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 is compatible.  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 is compatible.  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 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
2.0.0 82 3/3/2026
1.3.3 114 1/23/2026
1.3.2 111 1/22/2026
1.3.1 403 11/15/2025
1.3.1-beta3 287 11/12/2025
1.3.1-beta2 163 11/2/2025
1.3.1-beta1 242 10/14/2025
1.3.0 628 7/1/2025
1.3.0-beta9 240 9/29/2025
1.3.0-beta8 209 8/12/2025
1.3.0-beta7 364 5/21/2025
1.3.0-beta6 262 4/23/2025
1.3.0-beta5 163 4/19/2025
1.3.0-beta4 165 3/21/2025
1.3.0-beta3 272 3/19/2025
1.3.0-beta2 206 3/18/2025
1.3.0-beta1 158 1/12/2025
1.2.3 4,283 9/24/2024
1.2.2 2,205 6/18/2024
1.2.1 5,477 3/18/2023
Loading failed