This series of articles will give you ways to consume Web API by a ASP.NET MVC Client in .NET Core with different methods.
- Consume Web API By MVC In .NET Core (1), Server And Framework
- Consume Web API By MVC In .NET Core (2), HttpClient
- Consume Web API By MVC In .NET Core (3), HttpWebRequest
- Consume Web API By MVC In .NET Core (4), MS Clients --- this article
This article will discuss the three ways of consuming REST Web API provided by Microsoft.
Introduction
This is the content of the article:
- Introduction
- How to Consume RESTful APIs
- Three Ways Provided by .NET
- System.Net.WebRequest
- System.Net.WebRequest
- System.Net.WebClient
- Comparison between HttpWebRequest (HtppWebClient) and HttpClient
- WebRequest does all HttpClient does, but more complex, especially for Async calling
- WebRequest for Async calling
- HttpClient is the newest version, while WebRequest is obsolete
- Summary
How to Consume RESTful APIs
We can see the most comprehensive list of ways to consume RESTful APIs in your C# projects from this article A Few Great Ways to Consume RESTful API in C#, we borrowed here,
"There are several ways to consume a RESTful API in C#,
- HttpWebRequest/Response Class
- WebClient Class
- HttpClient Class
- RestSharp NuGet Package
- ServiceStack Http Utils
- Flurl
- DalSoft.RestClient
Every one of these has pros and cons."
Three Ways Provided by .NET
Inside of the seven ways, there are three ways provided by Microsoft .NET that we can consume Rest APIs.
- System.Net.WebRequest
- WebClient
- HttpClient
In a nutshell, WebRequest—in its HTTP-specific implementation, HttpWebRequest—represents the original way to consume HTTP requests in .NET Framework. WebClient provides a simple but limited wrapper around HttpWebRequest. And HttpClient is the new and improved way of doing HTTP requests and posts, having arrived with .NET Framework 4.5.
System.Net.WebRequest
The System.Net.WebRequest class is an abstract class. Thus you will need to create a HttpWebRequest or FileWebRequest to consume HTTP requests using this class.

WebRequest was the first class provided in the .NET Framework to consume HTTP requests. It gives you a lot of flexibility in handling each and every aspect of the request and response objects, without blocking the user interface thread. You can use this class to access and work with headers, cookies, protocols, and timeouts when working with HTTP.

System.Net.WebClient
The System.Net.WebClient class in .NET provides a high-level abstraction on top of HttpWebRequest. WebClient is just a wrapper around HttpWebRequest, so uses HttpWebRequest internally. Thus WebClient is a bit slow compared to HttpWebRequest, but requires you to write much less code. You can use WebClient for simple ways to connect to and work with HTTP services. It is generally a better choice than HttpWebRequest unless you need to leverage the additional features that HttpWebRequest provides. The following code snippet shows how you can work with WebClient.

HttpClient
HttpClient available .Net framework 4.5 or later versions. The HttpsClient provides functionality that neither the Webclient or HttpWenRequest does.
For example, with HttpsClient we can make multiple requests without having to create a new object.
Http class makes downloading files on separate threads easier. It is the preferred way to consume HTTP requests unless you have a specific reason not to use it. HttpClient combines the flexibility of HttpWebRequest and WebClient.
If you use either of them with async it should be good for the performance point of view as it will not block the resources waiting for the response and you will get good throughput.





Join the conversation! Your thoughts help the community grow.