WebApi系列~通過HttpClient來調(diào)用Web Api接口HttpClient是一個(gè)被封裝好的類,主要用于Http的通訊,它在.net,java,oc中都有被實(shí)現(xiàn),,當(dāng)然,,我只會.net,,所以,,只講.net中的HttpClient去調(diào)用Web Api的方法,,基于api項(xiàng)目的特殊性,,它需要有一個(gè)完全安全的環(huán)境,,所以,你的api控制器看起來有點(diǎn)特別,,只有5個(gè)方法,,而且都是標(biāo)準(zhǔn)的http方法,我覺得這種設(shè)計(jì)很不錯(cuò),,很清晰,,而且為了實(shí)現(xiàn)安全性,它不支持使用傳統(tǒng)的表單數(shù)據(jù),,取而代之的是FromBody參數(shù),,它指拿HttpRequestMessage里參數(shù),而不是所有的Request數(shù)據(jù),,這是基于安全方面的考慮,。 一 Api接口參數(shù)的標(biāo)準(zhǔn)性 Get方式,,可以有多個(gè)重載,有多個(gè)參數(shù) POST方式,,只能有一個(gè)參數(shù),,并且用[FromBody]約束,如果有多個(gè)參數(shù),,需要以對象的方式進(jìn)行傳遞 Put方式,,只能有兩個(gè)參數(shù),其中一個(gè)是通過Request.QueryString方式進(jìn)行傳遞的,,作為要更新對象的主鍵,,別一個(gè)是[FromBody]字段,也是一個(gè)字段,,如果多個(gè)字段需要把它封裝成對象 標(biāo)準(zhǔn)接口如圖 二 調(diào)用方,,參數(shù)的標(biāo)準(zhǔn)性 在客戶端進(jìn)行接口調(diào)用時(shí),我們以網(wǎng)頁端為例,,看一下網(wǎng)頁端進(jìn)行ajax跨域請求的代碼 Get方式 $.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' },//這里鍵名稱必須為空,,多個(gè)參數(shù)請傳對象,api端參數(shù)名必須為value success: function (data) { console.log("post:" + data); } }); 三 在控制臺中實(shí)現(xiàn)Get方式獲取接口數(shù)據(jù)(只有異步實(shí)現(xiàn)) /// <summary> /// HttpClient實(shí)現(xiàn)Get請求 /// </summary> static async void dooGet() { string url = "http://localhost:52824/api/register?id=1&leval=5"; //創(chuàng)建HttpClient(注意傳入HttpClientHandler) var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; using (var http = new HttpClient(handler)) { //await異步等待回應(yīng) var response = await http.GetAsync(url); //確保HTTP成功狀態(tài)值 response.EnsureSuccessStatusCode(); //await異步讀取最后的JSON(注意此時(shí)gzip已經(jīng)被自動解壓縮了,,因?yàn)樯厦娴腁utomaticDecompression = DecompressionMethods.GZip) Console.WriteLine(await response.Content.ReadAsStringAsync()); } } 四 在控制臺中實(shí)現(xiàn)Post方式提交數(shù)據(jù)(只有異步實(shí)現(xiàn)) /// <summary> /// HttpClient實(shí)現(xiàn)Post請求 /// </summary> static async void dooPost() { string url = "http://localhost:52824/api/register"; var userId = "1"; //設(shè)置HttpClientHandler的AutomaticDecompression var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; //創(chuàng)建HttpClient(注意傳入HttpClientHandler) using (var http = new HttpClient(handler)) { //使用FormUrlEncodedContent做HttpContent var content = new FormUrlEncodedContent(new Dictionary<string, string>() { {"", userId}//鍵名必須為空 }); //await異步等待回應(yīng) var response = await http.PostAsync(url, content); //確保HTTP成功狀態(tài)值 response.EnsureSuccessStatusCode(); //await異步讀取最后的JSON(注意此時(shí)gzip已經(jīng)被自動解壓縮了,,因?yàn)樯厦娴腁utomaticDecompression = DecompressionMethods.GZip) Console.WriteLine(await response.Content.ReadAsStringAsync()); } } 五 在控制臺中實(shí)現(xiàn)Put方式提交數(shù)據(jù)(只有異步實(shí)現(xiàn)) /// <summary> /// HttpClient實(shí)現(xiàn)Put請求 /// </summary> static async void dooPut() { string url = "http://localhost:52824/api/register?userid=" + userId; var userId = "1"; //設(shè)置HttpClientHandler的AutomaticDecompression var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; //創(chuàng)建HttpClient(注意傳入HttpClientHandler) using (var http = new HttpClient(handler)) { //使用FormUrlEncodedContent做HttpContent var content = new FormUrlEncodedContent(new Dictionary<string, string>() { {"", "數(shù)據(jù)"}//鍵名必須為空 }); //await異步等待回應(yīng) var response = await http.PutAsync(url, content); //確保HTTP成功狀態(tài)值 response.EnsureSuccessStatusCode(); //await異步讀取最后的JSON(注意此時(shí)gzip已經(jīng)被自動解壓縮了,因?yàn)樯厦娴腁utomaticDecompression = DecompressionMethods.GZip) Console.WriteLine(await response.Content.ReadAsStringAsync()); } } OK,到這里,,我們的客戶端如何去調(diào)用web api就講完了,,事實(shí)上,手機(jī)端,,平板端也是相關(guān)的方式去調(diào)用的,,它們也都有自己的HttpClient類,大同小異,!
|
|