回到目錄html
HttpClient是一個被封裝好的類,主要用於Http的通信,它在.net,java,oc中都有被實現,固然,我只會.net,因此,只講.net中的HttpClient去調用Web Api的方法,基於api項目的特殊性,它須要有一個徹底安全的環境,因此,你的api控制器看起來有點特別,只有5個方法,並且都是標準的http方法,我以爲這種設計很不錯,很清晰,並且爲了實現安全性,它不支持使用傳統的表單數據,取而代之的是FromBody參數,它指拿HttpRequestMessage裏參數,而不是全部的Request數據,這是基於安全方面的考慮。java
一 Api接口參數的標準性web
Get方式,能夠有多個重載,有多個參數ajax
POST方式,只能有一個參數,而且用[FromBody]約束,若是有多個參數,須要以對象的方式進行傳遞json
Put方式,只能有兩個參數,其中一個是經過Request.QueryString方式進行傳遞的,做爲要更新對象的主鍵,別一個是[FromBody]字段,也是一個字段,若是多個字段須要把它封裝成對象api
標準接口如圖跨域
二 調用方,參數的標準性安全
在客戶端進行接口調用時,咱們以網頁端爲例,看一下網頁端進行ajax跨域請求的代碼異步
Get方式async
$.ajax({ url: "http://localhost:52824/api/register", type: "GET", success: function (data) { console.log("json:" + data); } });
Post方式
$.ajax({ url: "http://localhost:52824/api/register", type: "POST", data: { '': '1' },//這裏鍵名稱必須爲空,多個參數請傳對象,api端參數名必須爲value success: function (data) { console.log("post:" + data); } });
三 在控制檯中實現Get方式獲取接口數據(只有異步實現)
/// <summary> /// HttpClient實現Get請求 /// </summary> static async void dooGet() { string url = "http://localhost:52824/api/register?id=1&leval=5"; //建立HttpClient(注意傳入HttpClientHandler) var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; using (var http = new HttpClient(handler)) { //await異步等待迴應 var response = await http.GetAsync(url); //確保HTTP成功狀態值 response.EnsureSuccessStatusCode(); //await異步讀取最後的JSON(注意此時gzip已經被自動解壓縮了,由於上面的AutomaticDecompression = DecompressionMethods.GZip) Console.WriteLine(await response.Content.ReadAsStringAsync()); } }
四 在控制檯中實現Post方式提交數據(只有異步實現)
/// <summary> /// HttpClient實現Post請求 /// </summary> static async void dooPost() { string url = "http://localhost:52824/api/register"; var userId = "1"; //設置HttpClientHandler的AutomaticDecompression var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; //建立HttpClient(注意傳入HttpClientHandler) using (var http = new HttpClient(handler)) { //使用FormUrlEncodedContent作HttpContent var content = new FormUrlEncodedContent(new Dictionary<string, string>() { {"", userId}//鍵名必須爲空 }); //await異步等待迴應 var response = await http.PostAsync(url, content); //確保HTTP成功狀態值 response.EnsureSuccessStatusCode(); //await異步讀取最後的JSON(注意此時gzip已經被自動解壓縮了,由於上面的AutomaticDecompression = DecompressionMethods.GZip) Console.WriteLine(await response.Content.ReadAsStringAsync()); } }
五 在控制檯中實現Put方式提交數據(只有異步實現)
/// <summary> /// HttpClient實現Put請求 /// </summary> static async void dooPut() { string url = "http://localhost:52824/api/register?userid=" + userId; var userId = "1"; //設置HttpClientHandler的AutomaticDecompression var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; //建立HttpClient(注意傳入HttpClientHandler) using (var http = new HttpClient(handler)) { //使用FormUrlEncodedContent作HttpContent var content = new FormUrlEncodedContent(new Dictionary<string, string>() { {"", "數據"}//鍵名必須爲空 }); //await異步等待迴應 var response = await http.PutAsync(url, content); //確保HTTP成功狀態值 response.EnsureSuccessStatusCode(); //await異步讀取最後的JSON(注意此時gzip已經被自動解壓縮了,由於上面的AutomaticDecompression = DecompressionMethods.GZip) Console.WriteLine(await response.Content.ReadAsStringAsync()); } }
OK,到這裏,咱們的客戶端如何去調用web api就講完了,事實上,手機端,平板端也是相關的方式去調用的,它們也都有本身的HttpClient類,大同小異!