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

时间:2022-10-22 08:52:31

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

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

7 个解决方案

#1


35  

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

#2


9  

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

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

#3


3  

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

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

#4


2  

Probably WCF; check out

可能是WCF;查看

http://hyperthink.net/blog/announcing-the-wcf-rest-starter-kit/

http://hyperthink.net/blog/announcing-the-wcf-rest-starter-kit/

#5


2  

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中进行了概念验证,服务堆栈应用程序更快(您可以在服务堆栈站点中查找测量值)并且更易于维护(代码更少,魔法实现更少)。最重要的是,它可以帮助您专注于简单性。

#6


1  

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视图或WCF,如@Brian所说。

#7


1  

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应用商店应用,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服务的一些示例的链接:从.net客户端调用web api

#1


35  

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

#2


9  

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

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

#3


3  

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

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

#4


2  

Probably WCF; check out

可能是WCF;查看

http://hyperthink.net/blog/announcing-the-wcf-rest-starter-kit/

http://hyperthink.net/blog/announcing-the-wcf-rest-starter-kit/

#5


2  

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中进行了概念验证,服务堆栈应用程序更快(您可以在服务堆栈站点中查找测量值)并且更易于维护(代码更少,魔法实现更少)。最重要的是,它可以帮助您专注于简单性。

#6


1  

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视图或WCF,如@Brian所说。

#7


1  

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应用商店应用,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服务的一些示例的链接:从.net客户端调用web api