ASP. 2 Lesson--httpclient Message Processor

Source: Internet
Author: User

Objective

A message processor is a class that receives an HTTP request and returns an HTTP response.

When compared to Representative, a series of message processing is linked together. The first processor receives an HTTP request, does some processing, and then passes the request to the next processor. At some point, the response is created and is traced back. This mode is called a delegate processor.

On the client side, theHTTPClient class uses a message handler to process the request. The default processor is Httpclienthandler, which sends requests over the network and gets a response from the server. You can insert a custom message processor into the client pipeline.

The ASP. NET Web API can also use the server-side message processor. For more information, refer to "HTTP Message Processor" (pending implementation.) )

Custom message handlers

To write a message handler, you need to derive from System.Net.Http.DelegatingHandler and override the SendAsync method. The following is the signature of the method:

System.threading.tasks.task

This method takes the httprequestmessage parameter as input and asynchronously returns a httpresponsemessage. A typical implementation is as follows:

1. Process the request message.

2. Call base. SendAsync sends the request to the internal processor.

3. The internal processor returns a response message. (This step is asynchronous)

4. Process the response and return it to the caller.

The following example shows a message handler that adds a custom header to an external request.

    public class Messagehandler:delegatinghandler    {        private int _count = 0;        protected override system.threading.tasks.task

To base. the invocation of SendAsync is asynchronous. If the processor has to do some work after the call, use the AWAIT keyword to continue execution after the method completes.

The following example shows a processor that logs an error code. How the log does not matter, but this example shows how to get the response inside the processor.

    public class Logginghandler:delegatinghandler     {         StreamWriter _writer;         Public Logginghandler (Stream stream)        {             _writer = new StreamWriter (stream);        }        protected override Async system.threading.tasks.task

Adding a message processor to a client pipeline

To add a custom processor to HttpClient, you use the httpclientfactory.create method:

HttpClient client = httpclientfactory.create (new MessageHandler ());

Message handlers are invoked in the order in which they are passed to the Create method. So the processor is inline, and the response message is passed in the opposite direction. That is, the last processor gets the response message first.

Summarize

This article mainly explains the HttpClient message processor. The code involved has been shown in the text, and will not be uploaded for the time being.

This article refers to the link http://www.asp.net/web-api/overview/web-api-clients/httpclient-message-handlers

ASP. 2 Lesson--httpclient Message Processor

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.