網站首頁 編程語言 正文
本篇體驗使用HttpClient消費ASP.NET Web API服務,例子比較簡單。
依次點擊"文件","新建","項目"。
選擇"ASP.NET Web API"項目。
在Models文件夾下創建Person.cs類。
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
在Controllers文件夾下創建一個空的PersonController。
public class PersonController : ApiController
{
}
創建一個符合管理的方法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
都可以獲取到數據。
在解決方案下創建一個控制臺應用程序。
在控制臺下引用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();
}
}
把控制臺程序設置為啟動項。點擊"啟動"。
如果想獲取xml格式,可以設置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類中,把發出請求的信息封裝到HttpRequestMessage中。
在控制臺應用程序引用如下:
System.Net.Http.dll
System.Net.Http.Formatting.dll
編寫如下:
static void Main(string[] args)
{
Console.WriteLine("獲取ASP.NET Web API服務內容如下:");
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);
}
以上就是創建簡單的ASP.NET Web API服務,以及使用WebClient和HttpClient消費服務的簡單例子。
原文鏈接:https://www.cnblogs.com/darrenji/p/4438473.html
相關推薦
- 2022-12-08 vscode擴展代碼定位實現步驟詳解_相關技巧
- 2022-08-25 C++詳細講解互斥量與lock_guard類模板及死鎖_C 語言
- 2022-04-15 C語言的程序環境與預處理你真的了解嗎_C 語言
- 2022-10-22 PyTorch中的CUDA的操作方法_python
- 2023-07-07 CreateObject創建vbs對象時不支持中文而報錯
- 2022-04-04 Server is Started at port : 5500,但是卻不能打開網頁
- 2023-03-02 Conan中的C/C++的依賴管理_C 語言
- 2022-10-24 golang程序進度條實現示例詳解_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支