How to Consume a Restful Service in .NET? [closed]

2019-01-13 15:33发布

What are my options to consume a RESTful service using the .Net framework? When is WCF(using the WebChannelFactory) more preferable to HttpClient?

标签: c# .net rest
7条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-13 15:44

I think WCF is preferable whenever you want the abstraction it provides.

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 also provides configuration mechanisms, extensibility points, and instrumentation.

查看更多
倾城 Initia
3楼-- · 2019-01-13 15:45

There are few different ways of consuming REST services in .NET:

I've wrote a blog post that demonstrates first three options.

As of consuming through WCF or HttpClient I think it makes sense to read this SO question to understand the potential of REST services. When you consume a REST service via WCF you cannot use all that power.

查看更多
时光不老,我们不散
4楼-- · 2019-01-13 15:48

The hammock project makes it very easy to consume RESTful services, you can use it to easily create the required http requests you need:

https://github.com/danielcrenna/hammock

查看更多
Melony?
5楼-- · 2019-01-13 15:54

I just released a REST client here today. You can download the Git repo to see the samples. 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).

When is WCF(using the WebChannelFactory) more preferable to 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.

查看更多
甜甜的少女心
6楼-- · 2019-01-13 15:56

Microsoft`s newest HTTP library is here https://www.nuget.org/packages/Microsoft.Net.Http and I have a blog post showing how to use it here.

You would never want to use WebChannelFactory against a RESTful service. The coupling generated by WebChannelFactory defeats the point of REST.

查看更多
贼婆χ
7楼-- · 2019-01-13 16:08

This is one technique of calling or consuming rest webservice in asp.net c#

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& user_id=test@gmail.com",ParameterType.RequestBody);

IRestResponse response = client.Execute(request);
查看更多
登录 后发表回答