C# 如何在 .NET 中使用 Restful 服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8883656/
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
How to Consume a Restful Service in .NET?
提问by user989046
What are my options to consume a RESTful service using the .Net framework? When is WCF(using the WebChannelFactory) more preferable to HttpClient?
使用 .Net 框架使用 RESTful 服务的选项有哪些?什么时候 WCF(使用 WebChannelFactory)比 HttpClient 更可取?
回答by Bassam Mehanni
The hammock project makes it very easy to consume RESTful services, you can use it to easily create the required http requests you need:
吊床项目使得使用 RESTful 服务变得非常容易,您可以使用它轻松创建所需的 http 请求:
回答by Rex Morgan
回答by Ethan Cabiac
I think WCF is preferable whenever you want the abstraction it provides.
我认为 WCF 在您想要它提供的抽象时更可取。
WCF provides an abstraction over the specific messaging and communication protocols being employed. Even only considering a RESTful scenario, you can more easily adapt to different message formats (XML, JSON, HTML).
WCF 提供了对正在使用的特定消息传递和通信协议的抽象。即使只考虑 RESTful 场景,您也可以更轻松地适应不同的消息格式(XML、JSON、HTML)。
WCF also provides configuration mechanisms, extensibility points, and instrumentation.
WCF 还提供配置机制、扩展点和检测。
回答by Darrel Miller
Microsoft`s newest HTTP library is here https://www.nuget.org/packages/Microsoft.Net.Httpand I have a blog post showing how to use it here.
微软最新的 HTTP 库在这里https://www.nuget.org/packages/Microsoft.Net.Http并且我有一篇博客文章展示了如何在此处使用它。
You would never want to use WebChannelFactory against a RESTful service. The coupling generated by WebChannelFactory defeats the point of REST.
您永远不会想对 RESTful 服务使用 WebChannelFactory。WebChannelFactory 生成的耦合破坏了 REST 的观点。
回答by Andriy Buday
There are few different ways of consuming REST services in .NET:
在 .NET 中使用 REST 服务有几种不同的方式:
- Plain .NET HTTP request
- WCF mechanisms
- HttpClient(recommended, nuget package)
- Other libraries (RestSharp, Hammock, etc.)
- 纯 .NET HTTP请求
- WCF机制
- HttpClient(推荐,nuget 包)
- 其他库(RestSharp、Hammock 等)
I've wrote a blog postthat demonstrates first three options.
我写了一篇博客文章,展示了前三个选项。
As of consuming through WCF or HttpClient I think it makes sense to read this SO questionto understand the potential of REST services. When you consume a REST service via WCF you cannot use all that power.
至于通过 WCF 或 HttpClient 消费,我认为阅读这个SO 问题以了解 REST 服务的潜力是有意义的。当您通过 WCF 使用 REST 服务时,您无法使用所有这些功能。
回答by Piyush Katkar
This is one technique of calling or consuming rest webservice in asp.net c#
这是在asp.net c#中调用或使用rest webservice的一种技术
var client = new RestClient("url");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/x-www-form-urlencoded",
"type=password& [email protected]",ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
回答by Melbourne Developer
I just released a REST client here today. You can download the Git repo to see the samples. https://bitbucket.org/MelbourneDeveloper/restclient-.net
我今天刚刚在这里发布了一个 REST 客户端。您可以下载 Git 存储库以查看示例。 https://bitbucket.org/MelbourneDeveloper/restclient-.net
- Open Source. (MIT License)
- Markup language agnostic. (Supports JSON, SOAP and other markup languages)
- Use strong types with REST.
- Supports Android, iOS, Windows 10, Windows 10 Phone, Silverlight, .NET, .NET Core.
- Incredibly simple.
- Async friendly (uses async, await keywords).
- 开源。(麻省理工学院许可证)
- 标记语言不可知。(支持 JSON、SOAP 等标记语言)
- 在 REST 中使用强类型。
- 支持 Android、iOS、Windows 10、Windows 10 Phone、Silverlight、.NET、.NET Core。
- 难以置信的简单。
- 异步友好(使用 async、await 关键字)。
When is WCF(using the WebChannelFactory) more preferable to HttpClient?
什么时候 WCF(使用 WebChannelFactory)比 HttpClient 更可取?
That is a very loaded question. WCF is a very large collection of technologies that allow you to communicate with a number of different protocols, authentication methods, and so on. It is very configurable, but REST is simple and supported by nearly all technologies available. If you write a REST service, chances are that nearly any app could consume it. Really, the question is about who your target audience is.
这是一个非常重要的问题。WCF 是一个非常大的技术集合,允许您与许多不同的协议、身份验证方法等进行通信。它是非常可配置的,但 REST 很简单,几乎所有可用的技术都支持它。如果您编写 REST 服务,那么几乎所有应用程序都可以使用它。实际上,问题在于您的目标受众是谁。

