使用HttpClient消費(fèi)ASP.NET Web API服務(wù)案例
本篇體驗(yàn)使用HttpClient消費(fèi)ASP.NET Web API服務(wù),例子比較簡單。
依次點(diǎn)擊"文件","新建","項(xiàng)目"。
選擇"ASP.NET Web API"項(xiàng)目。
在Models文件夾下創(chuàng)建Person.cs類。
public class Person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } }
在Controllers文件夾下創(chuàng)建一個空的PersonController。
public class PersonController : ApiController { }
創(chuàng)建一個符合管理的方法GetAllPersons。
public class PersonController : ApiController { public IEnumerable<Person> GetAllPersons() { return new List<Person> { new Person(){Id = 1, FirstName = "jack", LastName = "li"}, new Person(){Id = 2, FirstName = "darren", LastName = "ji"}, new Person(){Id = 3, FirstName = "sunny", LastName = "su"} }; } }
在瀏覽器中輸入:
http://localhost:2497/api/Person
http://localhost:2497/api/Person/AllPersons
都可以獲取到數(shù)據(jù)。
在解決方案下創(chuàng)建一個控制臺應(yīng)用程序。
在控制臺下引用System.Net,并編寫如下:
static void Main(string[] args) { using (WebClient proxy = new WebClient()) { var response = proxy.DownloadString("http://localhost:2497/api/Person"); Console.WriteLine(response); Console.ReadKey(); } }
把控制臺程序設(shè)置為啟動項(xiàng)。點(diǎn)擊"啟動"。
如果想獲取xml格式,可以設(shè)置WebClient的Headers屬性。
代碼修改如下:
static void Main(string[] args) { using (WebClient proxy = new WebClient()) { proxy.Headers.Add(HttpRequestHeader.Accept, "application/xml"); var response = proxy.DownloadString("http://localhost:2497/api/Person"); Console.WriteLine(response); Console.ReadKey(); } }
WebClient用起來似乎也不錯,不過,HttpClient具有更豐富的API。HttpClient把接收的信息封裝在HttpResponseMessage類中,把發(fā)出請求的信息封裝到HttpRequestMessage中。
在控制臺應(yīng)用程序引用如下:
System.Net.Http.dll
System.Net.Http.Formatting.dll
編寫如下:
static void Main(string[] args) { Console.WriteLine("獲取ASP.NET Web API服務(wù)內(nèi)容如下:"); HttpClient proxy = new HttpClient(); proxy.GetAsync("http://localhost:2497/api/Person").ContinueWith((previous) => { HttpResponseMessage response = previous.Result; response.Content.ReadAsStringAsync().ContinueWith((a) => { foreach (var item in a.Result) { Console.WriteLine(item.ToString()); } }); }); Console.ReadKey(true); }
以上就是創(chuàng)建簡單的ASP.NET Web API服務(wù),以及使用WebClient和HttpClient消費(fèi)服務(wù)的簡單例子。
到此這篇關(guān)于使用HttpClient消費(fèi)ASP.NET Web API服務(wù)的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asp.net 獲取Datalist中Checkbox的值的小結(jié)
最近開發(fā)過程中遇到一個小問題,要獲取checkbox的值,在網(wǎng)上搜索了一下,發(fā)現(xiàn)基本上都是用JS實(shí)現(xiàn)的,現(xiàn)在我將自己的做法記錄一下,以便以后繼續(xù)使用。2010-04-04擴(kuò)展了Repeater控件的EmptyDataTemplate模板功能
Repeater控件是一個數(shù)據(jù)顯示控件,該控件允許通過為列表中顯示的每一項(xiàng)重復(fù)使用指定的模板來自定義布局2013-01-01.NET或.NET Core Web APi基于tus協(xié)議實(shí)現(xiàn)斷點(diǎn)續(xù)傳的示例
這篇文章主要介紹了.NET或.NET Core Web APi基于tus協(xié)議實(shí)現(xiàn)斷點(diǎn)續(xù)傳的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11asp.net richTextBox中高亮顯示選中字符串或文本
最近開發(fā)程序需要對一段文本中的某個字符串進(jìn)行高亮顯示,網(wǎng)上找了下資料2011-11-11ASP.NET2.0數(shù)據(jù)庫入門之SQL Server
ASP.NET2.0數(shù)據(jù)庫入門之SQL Server...2006-09-09基于localStorge開發(fā)登錄模塊的記住密碼與自動登錄實(shí)例
下面小編就為大家?guī)硪黄趌ocalStorge開發(fā)登錄模塊的記住密碼與自動登錄實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08ASP.NET 2.0中預(yù)設(shè)的cookie
ASP.NET 2.0中預(yù)設(shè)的cookie...2006-09-09http轉(zhuǎn)https的實(shí)戰(zhàn)記錄(iis 7.5)
這篇文章主要給大家介紹了關(guān)于http轉(zhuǎn)https的相關(guān)資料,文中是最近的一次實(shí)戰(zhàn)記錄,基于iis7.5,通過一步步的圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2018-01-01ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)
本文主要介紹了ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07