使用 System.Net.Http.Json 简化 HttpClient 的使用

使用 System.Net.Http.Json 简化 HttpClient 的使用

Intro

从 .NET Core 3.1 开始,微软添加了一个 System.Net.Http.Json 的扩展,可以用来简化 HttpClient 的使用,看到在很多项目里还并未开始使用,所以想向大家介绍一下

Sample

PostAsJson/PutAsJson

PostAsJson sample:

const string url = "http://localhost:5000/api/values";
using var httpClient = new HttpClient();

using var response = await httpClient.PostAsJsonAsync(url, new Category()
{
    Id = 1,
    Name = "Test"
});
response.EnsureSuccessStatusCode();

PutAsJson:

using var response = await httpClient.PutAsJsonAsync(url, new Category()
{
    Id = 1,
    Name = "Test"
});
response.EnsureSuccessStatusCode();

简单来说就是会把一个对象变成 JSON request body

目前支持 Post 和 Put 方法 默认的序列化方式和 ASP.NET Core 是一致的,会变成 camalCase, 例如

91855386010b2bf3f905ce3430e78c5e.png


如果要自定义序列化,可以传入一个 JsonSerializerOptions,如:

using var response = await httpClient.PostAsJsonAsync(url, new Category()
{
    Id = 1,
    Name = "Test"
}, new JsonSerializerOptions());
response.EnsureSuccessStatusCode();
c581a53326809706fb84ece09f4351b8.png

可以看到这个例子中的 body 和之前有所不同了,这正是因为我们使用了自定义的 JsonSerializerOptions

GetFromJsonAsync

var result = await httpClient.GetFromJsonAsync<ResultModel>(url);
ArgumentNullException.ThrowIfNull(result);
Console.WriteLine($"{result.Status}: {result.ErrorMsg}");

和上面的 AsJson 相对应,这个是从 Response body 到一个对象,同样地也支持自定义 JsonSerializerOptions,可以自己尝试一下

ReadFromJsonAsync

using var response = await httpClient.PutAsJsonAsync(url, new Category()
{
    Id = 1,
    Name = "Test"
});
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<ResultModel>();
ArgumentNullException.ThrowIfNull(result);
Console.WriteLine($"{result.Status}: {result.ErrorMsg}");

直接从 HttpContent 中读取 json 对象

JsonContent.Create

using var content = JsonContent.Create(new Category() { Id = 1, Name = "Test" });
using var response = await httpClient.PostAsync(url, content);
response.EnsureSuccessStatusCode();

从一个对象构建 HttpContent ,同样支持自定义序列化

MoreSample

使用前后的一些对比:

40bc3d86c4dc6f923971575beda74229.png

3f09f1210e3c317e104f7a31ff59f756.png

8f696d5a71c8504bf717b6bf770af993.png

More

JSON现在已经非常的普遍了,这一扩展可以使得 HttpClient 处理 JSON 更为简单,而且从 .NET 6 开始已经包含在了框架中,不需要再手动引用 nuget 包了

在 .NET 7 中会增加一个 PatchAsJsonAsync 的扩展方法,目前发布的 Preview 1 已经可用,使用方法类似于 PostAsJsonAsync/PutAsJsonAsync,HttpMethod 是 Patch

另外觉得应该有一个类似于 GetFromJsonAsyncDeleteFromJsonAsync,提了一个 issue,感兴趣的可以关注一下:https://github.com/dotnet/runtime/issues/65617

如果返回的 response 状态码不是 2xx,GetFromJsonAsync 会抛异常,如果是不合法的 JSON 也会抛出异常

References

  • https://github.com/dotnet/runtime/issues/65617

  • https://github.com/dotnet/runtime/tree/main/src/libraries/System.Net.Http.Json/src/System/Net/Http/Json

  • https://github.com/WeihanLi/SamplesInPractice/blob/master/HttpClientTest/JsonExtensionSample.cs

  • https://github.com/OpenReservation/ReservationServer/commit/d07f9dc5ae292dd748d7f7a879898009c198d70d

技术群:添加小编微信并备注进群

小编微信:mm1552923   

公众号:dotNet编程大全      

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值