Consume Web API By MVC In .NET Core (4), .NET Clients

This series of articles will give you ways to consume Web API by a ASP.NET MVC Client in .NET Core with different methods.

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
    1. WebRequest does all HttpClient does, but more complex, especially for Async calling
    2. WebRequest for Async calling
    3. 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.

  1. System.Net.WebRequest
  2. WebClient
  3. 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.

HttpClient is preferred over HttpWebRequest due to async methods available out of the box and you would not have to worry about writing begin/end methods.

Basically when you use async call (using either of the class), it will not block the resources waiting for the response and any other request would utilise the resources to make further calls.

Another thing to keep in mind that you should not be using HttpClient in the 'using' block to allow reuse of same resources again and again for other web requests.

Comparison between HttpWebRequest (HtppWebClient) and HttpClient

1, WebRequest does all HttpClient does, but more complex, especially for Async calling.

From the previous article, Consume Web API By MVC In .NET Core (3), HttpWebRequest, we can see:

DELETE:

HttpWebRequest:

For DELETE, we need at least three lines of code to complete the action:

  • Create a request
  • Define the request Method as POST
  • Using GetResponse() method to get the action done --- writing data into database

HttpClient:

only one line code is needed to claim the delete method, to make the action done, and also to make it running asynchronously. 

HOST/GET

For HOST/GET, furthermore, besides what we did the three things as DELETE, we need one more step to serialize/unserialize the data and Get the request stream and bring it into database/client. This sample is for HOST serialization process:

2, WebRequest for Async calling

WebRequest does support async calling, but not as HttpClient as a built-in feature. Microsoft sample, Making Asynchronous Requests - .NET Framework | Microsoft Docs, shows the complexity:

3, HttpClient is the newest version, while WebRequest is obsolete

HttpClient available .Net framework 4.5 or later version.

Microsoft announced on 11/17/2021, that WebRequestWebClient, and ServicePoint classes are marked as obsolete and generate a SYSLIB0014 warning at compile time.

Summary

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.

Reference


Similar Articles