網(wǎng)站首頁 編程語言 正文
本篇體驗使用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ù)的簡單例子。
原文鏈接:https://www.cnblogs.com/darrenji/p/4437173.html
相關(guān)推薦
- 2022-05-20 淺談redis的過期時間設(shè)置和過期刪除機制_Redis
- 2022-09-26 nodemon安裝在開發(fā)環(huán)境(非全局安裝)報錯解決【兩種方式】
- 2024-07-18 Spring Security概述快速入門
- 2022-06-09 詳解Python中*args和**kwargs的使用_python
- 2022-07-29 C語言深入回顧講解結(jié)構(gòu)體對齊_C 語言
- 2022-12-24 Golang實現(xiàn)組合模式和裝飾模式實例詳解_Golang
- 2023-12-25 前后端驗證碼分析(字母&計算)
- 2023-06-05 Python?socket之TCP通信及下載文件的實現(xiàn)_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支