Common.HttpClients 1.3.0

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

Common.HttpClients

快速上手

安装nuget包后ConfigureServices里面添加

services.AddHttpClientService();

需要用到的地方直接注入IHttpClientHelper进行,例如下面测试用例

public class ApiHttpClientTest
{
    private readonly IHttpHelper _httpHelper;

    private readonly string Host = "https://echo.apifox.com";

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

    [Fact]
    public async Task Get_ReturnOk()
    {
        var result = await _httpHelper.GetAsync<string>(Host + "/get?q1=11&q2=22");
        Assert.NotNull(result);
    }
}

功能

下面示例已经注入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}" } });

版本更新记录

  • 1.3.0
    • 优化错误日志输出
  • 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 net5.0 was computed.  net5.0-windows was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.

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.3.1-beta1 29 10/14/2025
1.3.0 348 7/1/2025
1.3.0-beta9 167 9/29/2025
1.3.0-beta8 169 8/12/2025
1.3.0-beta7 336 5/21/2025
1.3.0-beta6 230 4/23/2025
1.3.0-beta5 127 4/19/2025
1.3.0-beta4 135 3/21/2025
1.3.0-beta3 235 3/19/2025
1.3.0-beta2 174 3/18/2025
1.3.0-beta1 121 1/12/2025
1.2.3 2,906 9/24/2024
1.2.2 2,056 6/18/2024
1.2.1 4,720 3/18/2023
1.2.0 2,103 11/12/2022
1.1.5 1,276 9/11/2022
1.1.4 505 8/23/2022
1.1.3 1,648 4/4/2022
1.1.2 6,412 2/2/2021
1.1.1 595 12/15/2020
1.1.0 581 11/11/2020
1.0.0 658 10/8/2020