PK.Http.Utilities
1.0.10
dotnet add package PK.Http.Utilities --version 1.0.10
NuGet\Install-Package PK.Http.Utilities -Version 1.0.10
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="PK.Http.Utilities" Version="1.0.10" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PK.Http.Utilities" Version="1.0.10" />
<PackageReference Include="PK.Http.Utilities" />
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 PK.Http.Utilities --version 1.0.10
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: PK.Http.Utilities, 1.0.10"
#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 PK.Http.Utilities@1.0.10
#: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=PK.Http.Utilities&version=1.0.10
#tool nuget:?package=PK.Http.Utilities&version=1.0.10
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
PK.Http.Utilities
HttpClientFactory扩展类
功能特性:
1.利用IHttpClientFactory创建HttpClient的扩展对象HttpClientExtension;
2.扩展包含两个请求方法:GetAsync和PostAsync,可支持各种类型数据的请求;
3.增加HttpResponseMessage的扩展方法GetRequestLogAsync,用于获取本次请求日志;
4.请求异常不会抛出错误,请求异常(如超时)会返回HttpStatusCode=600,仍然可使用HttpResponseMessage.IsSuccessStatusCode进行判断。
示例:
public class UnitTest1
{
/// <summary>
/// 测试Get请求
/// </summary>
/// <returns></returns>
[TestMethod]
public async Task TestGetAsync()
{
var httpClient = HttpClientFactoryHelper.CreateClient();
var resMessage = await httpClient.GetAsync("http://localhost:63447/Home/TestGet?name=test");
//获取请求日志
var log = await resMessage.GetRequestLogAsync();
if (resMessage.IsSuccessStatusCode)
{
var content = await resMessage.Content.ReadAsStringAsync();
Console.WriteLine(content);
}
else
{
Console.WriteLine(resMessage.ReasonPhrase);
}
}
/// <summary>
/// 测试Post请求
/// </summary>
/// <returns></returns>
[TestMethod]
public async Task TestPostAsync()
{
var httpClient = HttpClientFactoryHelper.CreateClient();
var resMessage = await httpClient.PostAsync("http://localhost:63447/Home/TestPost?name=test");
var log = await resMessage.GetRequestLogAsync();
if (resMessage.IsSuccessStatusCode)
{
var content = await resMessage.Content.ReadAsStringAsync();
Console.WriteLine(content);
}
else
{
Console.WriteLine(resMessage.ReasonPhrase);
}
}
/// <summary>
/// 测试json请求
/// </summary>
/// <returns></returns>
[TestMethod]
public async Task TestPostStringContentAsync()
{
var httpClient = HttpClientFactoryHelper.CreateClient();
using (var postBody = new StringContent(JsonConvert.SerializeObject(new {@name = "test"}), Encoding.UTF8,
"application/json"))
{
var resMessage = await httpClient.PostAsync("http://localhost:63447/api/value/TestPost", postBody);
var log = await resMessage.GetRequestLogAsync();
if (resMessage.IsSuccessStatusCode)
{
var content = await resMessage.Content.ReadAsStringAsync();
Console.WriteLine(content);
}
else
{
Console.WriteLine(resMessage.ReasonPhrase);
}
}
}
/// <summary>
/// 测试Post FormUrlEncodedContent请求
/// </summary>
/// <returns></returns>
[TestMethod]
public async Task TestPostFormUrlEncodedContentAsync()
{
var httpClient = HttpClientFactoryHelper.CreateClient();
using (var postBody = new FormUrlEncodedContent(new KeyValuePair<string, string>[]{new KeyValuePair<string, string>("name", "test")}))
{
var resMessage = await httpClient.PostAsync("http://localhost:63447/Home/TestPost", postBody);
var log = await resMessage.GetRequestLogAsync();
if (resMessage.IsSuccessStatusCode)
{
var content = await resMessage.Content.ReadAsStringAsync();
Console.WriteLine(content);
}
else
{
Console.WriteLine(resMessage.ReasonPhrase);
}
}
}
/// <summary>
/// 测试Post MultipartFormDataContent
/// </summary>
/// <returns></returns>
[TestMethod]
public async Task TestPostMultipartFormDataContentAsync()
{
var httpClient = HttpClientFactoryHelper.CreateClient();
using (var postBody = new MultipartFormDataContent())
{
string boundary = string.Format("--{0}", DateTime.Now.Ticks.ToString("x"));
postBody.Headers.Add("ContentType", $"multipart/form-data, boundary={boundary}");
//data为请求文件接口需要的参数,根据调用接口参数而定
StringContent data = new StringContent(JsonConvert.SerializeObject(new {@name = "test"}));
postBody.Add(data, "data");
StringContent data1 = new StringContent(JsonConvert.SerializeObject(new { @name1 = "test1" }));
postBody.Add(data1, "data1");
var resMessage = await httpClient.PostAsync("http://localhost:63447/Home/TestMultipartContent", postBody);
var log = await resMessage.GetRequestLogAsync();
if (resMessage.IsSuccessStatusCode)
{
var content = await resMessage.Content.ReadAsStringAsync();
Console.WriteLine(content);
}
else
{
Console.WriteLine(resMessage.ReasonPhrase);
}
}
}
}
| 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. 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 | 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.Extensions.DependencyInjection (>= 3.1.0)
- Microsoft.Extensions.Http (>= 2.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.