如何使用HttpClient高效地消费ASP.NET WebAPI服务并构建长尾词?

2026-03-30 10:301阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计551个文字,预计阅读时间需要3分钟。

如何使用HttpClient高效地消费ASP.NET WebAPI服务并构建长尾词?

本篇体验使用HttpClient调用ASP.NET Web API服务,示例简单。

点击文件,新建项目。选择ASP.NET Web API项目。

在Models文件夹下创建Person.cs类。

csharppublic class Person{ public int Id { get; set; }}

本篇体验使用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"} }; } }

在浏览器中输入:

localhost:2497/api/Person
localhost:2497/api/Person/AllPersons

都可以获取到数据。

在解决方案下创建一个控制台应用程序。

在控制台下引用System.Net,并编写如下:

static void Main(string[] args) { using (WebClient proxy = new WebClient()) { var response = proxy.DownloadString("localhost:2497/api/Person"); Console.WriteLine(response); Console.ReadKey(); } }

把控制台程序设置为启动项。点击"启动"。

如果想获取xml格式,可以设置WebClient的Headers属性。

如何使用HttpClient高效地消费ASP.NET WebAPI服务并构建长尾词?

代码修改如下:

static void Main(string[] args) { using (WebClient proxy = new WebClient()) { proxy.Headers.Add(HttpRequestHeader.Accept, "application/xml"); var response = proxy.DownloadString("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("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消费服务的简单例子。

到此这篇关于使用HttpClient消费ASP.NET Web API服务的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计551个文字,预计阅读时间需要3分钟。

如何使用HttpClient高效地消费ASP.NET WebAPI服务并构建长尾词?

本篇体验使用HttpClient调用ASP.NET Web API服务,示例简单。

点击文件,新建项目。选择ASP.NET Web API项目。

在Models文件夹下创建Person.cs类。

csharppublic class Person{ public int Id { get; set; }}

本篇体验使用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"} }; } }

在浏览器中输入:

localhost:2497/api/Person
localhost:2497/api/Person/AllPersons

都可以获取到数据。

在解决方案下创建一个控制台应用程序。

在控制台下引用System.Net,并编写如下:

static void Main(string[] args) { using (WebClient proxy = new WebClient()) { var response = proxy.DownloadString("localhost:2497/api/Person"); Console.WriteLine(response); Console.ReadKey(); } }

把控制台程序设置为启动项。点击"启动"。

如果想获取xml格式,可以设置WebClient的Headers属性。

如何使用HttpClient高效地消费ASP.NET WebAPI服务并构建长尾词?

代码修改如下:

static void Main(string[] args) { using (WebClient proxy = new WebClient()) { proxy.Headers.Add(HttpRequestHeader.Accept, "application/xml"); var response = proxy.DownloadString("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("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消费服务的简单例子。

到此这篇关于使用HttpClient消费ASP.NET Web API服务的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持易盾网络。