In this Video, I am going to discuss HTTP Client Message Handler in Web API with real-time examples. As we alwatchy discussed in HTTP Message Handler Article that a Message Handler is a class that receives an HTTP request and returns an HTTP response. The Message handler is derived from the abstract HttpMessageHandler class. There are two types of HTTP Message Handlers as follows
How to implement HTTP Client Message Handler in Web API with Real-Time?. The Complete ASP.NET Web API Developer Course 2022 [Videos].
- The Server Side HTTP Message Handlers – we alwatchy discussed
- Client Side HTTP Message Handlers – will discuss in this Video
HTTP Client Message Handlers in Web API
The HttpClient class uses a message handler to process the requests on the client side. The default handler provided by the dot net framework is HttpClientHandler. This HTTP Client Message Handler sends the request over the network and also gets the response from the server. As a developer if you want, then you can also create your own custom message handlers and then insert the custom message handlers into the pipeline in the client side as shown in the below image.
Creating a Custom HTTP Client Message Handler:
Let us discuss how to create a Custom HTTP Client Message Handler. To create a custom HTTP Client message handler, what we need to do is, we need to create a custom class and that class should be derived from the System.Net.Http.DelegatingHandler class. Then the class should override the SendAsync method. The signature of the SendAsync method as following:
The SendAsync method takes an HttpRequestMessage as input and asynchronously returns an HttpResponseMessage. A typical implementation does the following:
- Process the request message.
- Call the base.SendAsync method to send the request to the inner handler.
- The inner handler returns a response message. (This step is asynchronous.)
- Process the response message and returns the response to the caller.
Creating a Custom Message Handler:
The following example shows the creation of a custom message handler which adds a custom header to the outgoing request:
The call to the base.SendAsync method is asynchronous. If your handler going to do some work after this call, then use the await keyword to resume execution after the method completes.
The following example shows a handler that logs error codes. The example shows how to get at the response inside the handler.
Adding Message Handlers to the Client Pipeline
To add a custom message handlers to HttpClient pipeline, we need to use the HttpClientFactory.Create method as shown below.
HttpClient client = HttpClientFactory.Create(new MessageHandler1(), new MessageHandler2());
The Message handlers are called in the order that we pass them into the Create method of the HttpClientFactory class. The reason is handlers are nested the response message travels in the other direction. That is, the last handler is the first to get the response message.
In the next Video, I am going to discuss How to Implement the Token Based Authentication in ASP.NET Web API. Here, in this Video, I try to explain HTTP Client Message Handler in Web API with some examples. I hope this Video will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this Video.