HttpClient 調用 RestAPI 接口的用法


Dotnet 源代碼 github地址:https://github.com/dotnet/core/tree/v5.0.0

HttpClient 是否Dotnet 客戶端調用 RestAPI 接口的類,開源的還有 RestSharp (https://restsharp.dev),

HttpClient 的官方使用文檔:https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=net-5.0

網文參考:  "優雅通過HttpClientFactory使用HttpClient" 。

                    “Create HTTPClient using IHttpClientFactory in ASP.NET Core” 

 

網上有一篇介紹使用的文章:   揭秘通用平台的 HttpClient (譯),   調用邏輯圖示: 

     

 全部來自: 幾個主要類(在 System.Net.Http 空間中):

 HttpClient :  繼承於  HttpMessageInvoker  (重點是:構造器 HttpMessageInvoker(HttpMessageHandler, [Boolean]),與方法 Send[Async](HttpRequestMessage, CancellationToken)

  屬性BaseAddress、 TimeoutMaxResponseContentBufferSize(long整數)、

                                  DefaultRequestHeadersDefaultRequestVersionDefaultVersionPolicyDefaultProxy    幾個默認屬性值

  方法: CancelPendingRequests()、下列方法全部依賴: SendAsync (HttpRequestMessage, CancellationToken)

           DeleteAsync(String/Uri, CancellationToken)

           PatchAsync(String/Uri, HttpContent, CancellationToken)

           PostAsync((String/Uri, HttpContent, CancellationToken)

           PutAsync(String/Uri, HttpContent, CancellationToken)、下列四個同類 Get 。

           GetAsync(String/Uri, HttpCompletionOption, CancellationToken)GetByteArrayAsync(String/Uri, CancellationToken)

           GetStringAsync(String/Uri, CancellationToken)GetStreamAsync(String/Uri, CancellationToken)   

擴展GetFromJsonAsync(HttpClient, String/Uri, Type, JsonSerializerOptions, CancellationToken)

          GetFromJsonAsync<TValue>(HttpClient, String/Uri, JsonSerializerOptions, CancellationToken)

            PostAsJsonAsync<TValue>(HttpClient, String/Uri, TValue, JsonSerializerOptions, CancellationToken)

             PutAsJsonAsync<TValue>(HttpClient, String/Uri, TValue, JsonSerializerOptions, CancellationToken)
  

HttpClientHandler : 繼承於 HttpMessageHandler(抽象) ,  派生了:WebRequestHandler

    (此文:將HttpClientHandler傳遞給HttpClient時調用哪個SendAsync方法   ---- 解釋了調用順序。)

     (另一E文   HttpClient, HttpClientHandler, and WebRequestHandler Explained  解釋   ---- 介紹三者  )

      (參考:How to use HttpClientHandler with IHttpClientFactory ?    )

方法繼承    HttpResponseMessage  Send(HttpRequestMessage request, CancellationToken cancellationToken);   ===   
           繼承   Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken);
屬性: 


                                         

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM