Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Create a custom message handler in Aspose.HTML for .NET by deriving from MessageHandler, overriding Invoke(), and adding the handler to the INetworkService.MessageHandlers pipeline before loading an HTMLDocument.
By definition, a message handler is a class that receives a Web request and returns a response. In other words, a message handler is used to process a Web service request during input and/or to process the response during output. The chain of handlers is a pipeline, which will all have the chance to process an outgoing Web request before it is sent or process a Web response.
In traditional client-server calls, the client sends a Web request to the server, waits for the result, then gets a response. There are message handlers both server-side and client-side, and sometimes you have to configure custom message handlers. Handler behavior is governed by a set of protocols that describe what action message handlers can take in a given situation. The handlers, their protocols and their place in the chain may be defined by a system or by a user.
People like customization, and sometimes they need to perform a custom logic in a request and response. You can add custom handlers to the pipeline. Each handler has a choice of actions that it can achieve. For example, the handler can be invoked only for request processing or only for response processing.
One or more custom message handlers can be created and organized in a chain of message handlers that get called with every request and response. Each Web request goes through a message handler pipeline, and we can create custom message handlers and extend the behavior of an existing pipeline. Graphically, the result looks something like this:

A message handler can return a response immediately. For example, you could create a handler that checks if a header is present on outgoing requests. If the header is missing, it doesn’t pass the request along to the next handler and generates an error response which it returns to the caller.

You can define the handler chain as a pipeline that will be able to process the outgoing Web request before sending it. For example, these handlers may check the request’s body or log some information about the request. The following example shows the key concepts of creating a custom message handler.
Aspose.HTML for .NET lets you create custom message handlers for network processing. The example below develops a simple LogMessageHandler that prints a message when a request starts and finishes.
To create and use a custom message handler:
Configuration, get INetworkService, and add the handler to the MessageHandlers collection.HTMLDocument with the configuration so the custom handler participates in the request pipeline.1using Aspose.Html.Net;
2...
3
4 public class LogMessageHandler : MessageHandler
5 {
6
7 }After you define the LogMessageHandler class, implement its processing logic in Invoke().
1// Implement a message handler that prints a message about starting and finishing processing request
2
3public class LogMessageHandler : MessageHandler
4{
5 // Override the Invoke() method
6 public override void Invoke(INetworkOperationContext context)
7 {
8 Debug.WriteLine("Start processing request: " + context.Request.RequestUri);
9
10 // Invoke the next message handler in the chain
11 Next(context);
12
13 Debug.WriteLine("Finish processing request: " + context.Request.RequestUri);
14 }
15}Add LogMessageHandler to the existing message handler pipeline. Use the Insert() method to place LogMessageHandler first in the collection of message handlers.
The following code snippet demonstrates how to add the LogMessageHandler to the pipeline and use it for a simple operation.
1// Log network requests for HTML processing in C#
2
3// Create an instance of the Configuration class
4using Configuration configuration = new Configuration();
5
6// Add the LogMessageHandler to the chain of existing message handlers
7INetworkService service = configuration.GetService<INetworkService>();
8MessageHandlerCollection handlers = service.MessageHandlers;
9
10handlers.Insert(0, new LogMessageHandler());
11
12// Prepare path to a source document file
13string documentPath = Path.Combine(DataDir, "input.htm");
14
15// Initialize an HTML document with specified configuration
16using HTMLDocument document = new HTMLDocument(documentPath, configuration);A custom message handler should inherit from the MessageHandler class in the Aspose.Html.Net namespace.
Override Invoke() to inspect or modify the network operation context, then continue the pipeline or return a response according to your workflow.
Use Insert() when the handler must run before existing handlers. Use Add() when it should run after the current handlers in the collection.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.