Common.HttpClients 1.3.0-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.0-beta2
                    
NuGet\Install-Package Common.HttpClients -Version 1.3.0-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.0-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.0-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.0-beta2
                    
#r "nuget: Common.HttpClients, 1.3.0-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.0-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.0-beta2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Common.HttpClients&version=1.3.0-beta2&prerelease
                    
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);

请求示例

var bytes = await File.ReadAllBytesAsync("D:\\2222.png");

var form = new MultipartFormDataContent();
var byteContent = new ByteArrayContent(bytes);
byteContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
    Name = "file",
    FileName = "img.png"
};
form.Add(byteContent, "file");
form.Add(new StringContent("img.png"), "FileName");
form.Add(new StringContent("image/png"), "ContentType");
var response = await client.PostFormDataAsync<string>("api/file", form);

版本更新记录

  • 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
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