国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

ASP.NET延遲調(diào)用或多次調(diào)用第三方Web API服務(wù)

瀏覽:148日期:2022-06-08 15:43:40

本篇體驗使用HttpClient消費ASP.NET Web API服務(wù),例子比較簡單。

依次點擊"文件","新建","項目"。

選擇"ASP.NET Web API"項目。

在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è)置為啟動項。點擊"啟動"。

如果想獲取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消費服務(wù)的簡單例子。

到此這篇關(guān)于ASP.NET延遲調(diào)用或多次調(diào)用第三方Web API服務(wù)的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持。

標簽: ASP.NET
相關(guān)文章:
主站蜘蛛池模板: 欧美一区二区三区视频 | 亚洲美女中文字幕 | 黄色美女视频免费看 | 中文字幕亚洲视频 | 免费观看成人www精品视频在线 | 国产一区二区三区不卡免费观看 | 一区二区三区在线播放视频 | 久久久久久久综合色一本 | 欧美13一14周岁a在线播放 | jiz欧美高清 | 草草免费观看视频在线 | 偷拍亚洲欧美 | 可以免费看黄的网址 | 五月色一区二区亚洲小说 | 亚洲永久中文字幕在线 | 国产在线不卡免费播放 | 香焦视频在线观看黄 | 午夜毛片视频高清不卡免费 | 国内久久精品视频 | 亚洲免费在线视频 | 中文字幕亚洲一区二区va在线 | 久久视频在线 | 免费一级α片在线观看 | 国产三级一区二区 | 成人欧美一区二区三区 | 国产成人午夜精品免费视频 | 免费视频网站一级人爱视频 | 美女张开腿让男人捅的视频 | 亚洲一区二区三区首页 | 美女被男人桶到嗷嗷叫爽网站 | 99久久香蕉国产线看观香 | 欧美精品成人一区二区在线观看 | 2021国产精品系列一区二区 | 全部毛片 | 欧美一级片手机在线观看 | 国产精品亚洲第一区柳州莫青 | 久久国产精品免费观看 | 在线观看欧洲成人免费视频 | 久久riav.com| 一级毛片欧美大片 | 欧美一级特黄aaa大片 |