久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

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

使用HttpClient消費(fèi)ASP.NET Web API服務(wù)案例

瀏覽:9日期:2022-06-08 15:51:29

本篇體驗(yàn)使用HttpClient消費(fèi)ASP.NET Web API服務(wù),例子比較簡(jiǎn)單。

依次點(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)建一個(gè)空的PersonController。

    public class PersonController : ApiController    {    }

創(chuàng)建一個(gè)符合管理的方法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)建一個(gè)控制臺(tái)應(yīng)用程序。

在控制臺(tái)下引用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();    }}

把控制臺(tái)程序設(shè)置為啟動(dòng)項(xiàng)。點(diǎn)擊"啟動(dòng)"。

如果想獲取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用起來(lái)似乎也不錯(cuò),不過(guò),HttpClient具有更豐富的API。HttpClient把接收的信息封裝在HttpResponseMessage類中,把發(fā)出請(qǐng)求的信息封裝到HttpRequestMessage中。

在控制臺(tái)應(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)建簡(jiǎn)單的ASP.NET Web API服務(wù),以及使用WebClient和HttpClient消費(fèi)服務(wù)的簡(jiǎn)單例子。

到此這篇關(guān)于使用HttpClient消費(fèi)ASP.NET Web API服務(wù)的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持。

標(biāo)簽: ASP.NET
相關(guān)文章:
主站蜘蛛池模板: 成人a视频| 欧美成人在线免费视频 | 国产精品亚洲一区二区三区在线 | 太平公主一级艳史播放高清 | 国产91在线视频 | 91久久精品日日躁夜夜躁欧美 | 最新av网址大全 | 日本一级中文字幕久久久久久 | 午夜免费剧场 | 中文字幕av亚洲精品一部二部 | 高清av网站| 久久密 | 精品成人在线 | 午夜家庭影院 | 国产欧美日韩精品在线 | 九九天堂网 | 国产精品亚洲精品久久 | 免费观看视频毛片 | 久久免费看 | 国产精品亚洲综合 | 在线播放亚洲 | 亚洲一区中文字幕在线观看 | 免费观看的av | 久久久999精品视频 五月天婷婷在线视频 | 国产精品毛片久久久久久久 | 欧洲毛片 | 欧美久久综合 | 成年人视频在线免费观看 | 国产精久| 中文字幕在线播放一区 | 国产一区二区三区精品久久久 | 欧美日韩国产一区二区三区 | 亚洲精区 | 天堂亚洲网 | 亚洲一区二区在线视频 | 中文字幕乱码亚洲精品一区 | 大象一区 | 成人av播放 | 日韩a∨精品日韩在线观看 山岸逢花在线 | 精品九九| 欧美日韩综合一区 |