消费REST Web服务的最佳方法是什么? [英] What is the best way to consume REST web services?
问题描述
从.NET使用REST Web服务的最佳方法是什么?
What's the best way to consume REST web services from .NET?
推荐答案
一种简单而直接的方法是使用System.Net命名空间中的WebClient.
A straight forward and easy approach would be to use WebClient which is in the System.Net namespace.
几乎所有您需要做的就是以查询字符串的形式传入所需的Uri以及任何所需的参数,并且您应该以字符串形式(JSON或xml)获取响应.例如.
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.
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));
然后,就像Nick提到的那样,您可以根据需要使用XmlDocument或JavaScriptSerializer来操纵结果.无论如何,我建议您查看其中的文档,看看它是否满足您的需求. http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx
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
这篇关于消费REST Web服务的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!