.net 使用 REST Web 服务的最佳方式是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/329256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:43:00  来源:igfitidea点击:

What is the best way to consume REST web services?

.netweb-servicesrest

提问by

What's the best way to consume REST web services from .NET?

从 .NET 使用 REST Web 服务的最佳方式是什么?

回答by Shane K

A straight forward and easy approach would be to use WebClient which is in the System.Net namespace.

一种直接且简单的方法是使用 System.Net 命名空间中的 WebClient。

Pretty much all you need to do is pass in the Uri required with any parameters needed in the form of a query string and you should get back the response in the form of a string, be it json or xml. For example.

几乎所有您需要做的就是以查询字符串的形式传入所需的 Uri 和所需的任何参数,您应该以字符串的形式返回响应,无论是 json 还是 xml。例如。

using System.Net; 

string param = "hello";

string url = String.Format("http://somedomain.com/samplerequest?greeting={0}",param);

WebClient serviceRequest = new WebClient();
string response = serviceRequest.DownloadString(new Uri(url));

Then, like Nick mentioned, you can use XmlDocument or JavaScriptSerializer to manipulate the results as needed. Anyway I suggest checking out the documentation on it to see if it meets your needs. http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx

然后,就像 Nick 提到的那样,您可以根据需要使用 XmlDocument 或 JavaScriptSerializer 来操作结果。无论如何,我建议查看它的文档,看看它是否满足您的需求。http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx

回答by Custodio

Instead using WebClient like Kenney, you can use HttpWebRequest and HttpWebResponse and process the result with a StreamReader and XmlDocument .

您可以使用 HttpWebRequest 和 HttpWebResponse 并使用 StreamReader 和 XmlDocument 处理结果,而不是像 Kenney 这样使用 WebClient。

回答by Dani

There is also RestSharp, a lightweight .NET component that lets you easily consume REST web services

还有RestSharp,这是一个轻量级的 .NET 组件,可让您轻松使用 REST Web 服务

回答by Rafa

In my opinion, the easiest way to implement a REST API is to use Service Stack:

在我看来,实现 REST API 的最简单方法是使用服务堆栈:

http://www.servicestack.net/

http://www.servicestack.net/

I my last Windows project I made a proof of concept in WCF and ServiceStack and the Service Stack application was faster (you can look for measurements in the Service Stack site) and easier to maintain (less code, less magic implementation). And the most important point, it helps you to focus in simplicity.

我的上一个 Windows 项目我在 WCF 和 ServiceStack 中进行了概念验证,并且 Service Stack 应用程序更快(您可以在 Service Stack 站点中查找测量值)并且更易于维护(更少的代码,更少的魔术实现)。最重要的一点是,它可以帮助您专注于简单。

回答by Nick Berardi

Do you want to consume or publish. If you want to consume, such as making requests the best way to interact with it is to figure out the type it will comback as, usually JSON or XML. After you have your type you can use XmlDocument or JavaScriptSerializer to pull back the information and use it.

你是要消费还是要发布。如果您想使用,例如发出请求,与其交互的最佳方式是确定它将返回的类型,通常是 JSON 或 XML。获得类型后,您可以使用 XmlDocument 或 JavaScriptSerializer 拉回信息并使用它。

If you want to produce a REST interface then you probably want to use either MVC a REST View or WCF as @Brian said.

如果您想生成 REST 接口,那么您可能想使用 MVC REST View 或 WCF,如@Brian 所说。

回答by santos

If the REST services were built using ASP.NET Web API then I would use the Microsoft Http Client Libraries. (nuget package available). N.B. This appears to have superseded the web api client libraries that are listed in the nuget screenshot on the link below.

如果 REST 服务是使用 ASP.NET Web API 构建的,那么我将使用 Microsoft Http 客户端库。(nuget 包可用)。注意这似乎已经取代了下面链接的 nuget 屏幕截图中列出的 web api 客户端库。

This will work from .NET 4, Windows Store Apps, Windows Phone 7.5/8, Silverlight 4 and 5.

这适用于 .NET 4、Windows Store Apps、Windows Phone 7.5/8、Silverlight 4 和 5。

In theory you could use it to call any REST service built with other frameworks as well.

理论上,您也可以使用它来调用使用其他框架构建的任何 REST 服务。

Here's a link with some samples on using the HttpClient class to call REST services: Calling a web api from a .net client

以下是一些有关使用 HttpClient 类调用 REST 服务的示例的链接: Calling a web api from a .net client