免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
HttpClient來自官方的JSON擴(kuò)展方法

System.Net.Http.Json

Json的序列化和反序列化是我們?nèi)粘3R姷牟僮?,通過System.Net.Http.Json我們可以用少量的代碼實(shí)現(xiàn)上述操作.正如在github設(shè)計(jì)文檔中所描述

Serializing and deserializing JSON payloads from the network is a verycommon operation for clients, especially in the upcoming Blazorenvironment. Right now, sending a JSON payload to the server requiresmultiple lines of code, which will be a major speed bump for thosecustomers. We'd like to add extension methods on top of HttpClient thatallows doing those operations with a single method call.

他的依賴項(xiàng)也非常的少目前只依賴System.Net.Http, System.Text.Json
System.Text.Json相對(duì)于Newtonsoftjson平均快了兩倍,如果有興趣相關(guān)基準(zhǔn)測(cè)試可在這個(gè)文章中查閱
https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-apis/

在.NET中安裝和使用

目前它還是預(yù)覽版本

dotnet add package System.Net.Http.Json
public static async Task<Customer> GetCustomerAsync(){    HttpClient clinet=new HttpClient();    var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:5000/customers");    var response = await clinet.SendAsync(request);    return await response.Content.ReadFromJsonAsync<Customer>();}

通過ReadFromJsonAsync直接可以反序列化

public static async Task<Customer> CreateCustomerAsync(){    HttpClient clinet = new HttpClient();    var customer=new Customer()    {        Id = "1",        Name = "Fh"    };    var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost:5000/create");    request.Content = JsonContent.Create(customer);    var response = await clinet.SendAsync(request);    var content=response.Content.ReadAsStringAsync();    return customer;}

還可以以下面這種簡(jiǎn)潔方式使用

_client.GetFromJsonAsync<IReadOnlyList<Customer>>("/customers");_client.GetFromJsonAsync<Customer?>($"/customers/{id}");_client.PutAsJsonAsync($"/customers/{customerId}", customer);
if (response.IsSuccessStatusCode){    try    {        return await response.Content.ReadFromJsonAsync<User>();    }    catch (NotSupportedException) // When content type is not valid    {        Console.WriteLine("The content type is not supported.");    }    catch (JsonException) // Invalid JSON    {        Console.WriteLine("Invalid JSON.");    }}

還可以通過NotSupportedException和JsonException異常類處理相應(yīng)的異常.

Reference

https://github.com/hueifeng/BlogSample/tree/master/src/SYSTEMNETHTTPJSON

https://www.stevejgordon.co.uk/sending-and-receiving-json-using-httpclient-with-system-net-http-json 

https://github.com/dotnet/designs/blob/d4018c99c8134e9114a869e2e73a050059b9e663/accepted/2020/json-http-extensions/json-http-extentions.md

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
WebApi系列~通過HttpClient來調(diào)用Web Api接口
android以application/json流的方式提交數(shù)據(jù)
HttpClient詳細(xì)使用示例
Jquery的.post說解(二)
jQuery能做到,PHP能做到,C#也能做到
HttpClient + ASP.NET Web API, WCF之外的另一個(gè)選擇
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服