最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2022年10月17日 11:32:38   作者:Darren?Ji  
這篇文章介紹了ASP.NET延遲調(diào)用或多次調(diào)用第三方Web?API服務(wù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

本篇體驗(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)建一個(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用起來似乎也不錯(cuò),不過,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)建簡單的ASP.NET Web API服務(wù),以及使用WebClient和HttpClient消費(fèi)服務(wù)的簡單例子。

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

相關(guān)文章

最新評(píng)論

正宁县| 安国市| 美姑县| 大兴区| 白山市| 高淳县| 长治市| 尚义县| 青河县| 开江县| 资兴市| 松江区| 通州区| 苍梧县| 靖安县| 赞皇县| 盐源县| 漠河县| 洛川县| 广德县| 阜康市| 于都县| 岳普湖县| 蒲江县| 沁源县| 资中县| 衡水市| 都江堰市| 固始县| 伽师县| 平湖市| 凌云县| 泾源县| 鱼台县| 酒泉市| 阿荣旗| 莱芜市| 通江县| 宁晋县| 山阴县| 明光市|