Message Handler to Filter Network Requests – C# examples

Using message handlers is a typical pattern in software development. Message handlers are used to intercept and process messages in a pipeline manner. In this article, we will create custom message handlers that filters network requests and disables unwanted ones. You find simple C# examples of how to apply message handlers for network operations.

Network Requests

In software engineering, network requests refer to a client application sending a request to a server over the network and receiving a response from the server. Message handlers are a powerful way to intercept and process these network requests and responses.
This article considers examples of intercepting requests, blocking some unwanted network requests, or replacing the URLs with some other value.

Message Handler to Filter Network Requests

Aspose.HTML for .NET library provides functionality that can be useful in scenarios where you want to restrict certain types of network requests in an HTML processing application. For example, if you work with HTML documents and want to ensure that only certain types of resources are loaded from external sources. This code provides a way to filter out undesired network requests. It sets up a NetworkDisabledMessageHandler that only allows requests with the “file:”, “about:” or “base64:” protocols. If the request uses any other protocol, the handler blocks it.

 1// Define the NetworkDisabledMessageHandler class that is derived from the MessageHandler class
 2public class NetworkDisabledMessageHandler : MessageHandler
 3{
 4    // Override the Invoke() method
 5    public override void Invoke(INetworkOperationContext context)
 6    {
 7        if (context.Request.RequestUri.Protocol == "file:" ||
 8            context.Request.RequestUri.Protocol == "base64:" ||
 9            context.Request.RequestUri.Protocol == "about:")
10            Next(context);
11    }
12}

The above C# code snippet creates a message handler to customize the behavior of network requests based on their protocols:

This custom message handler can filter out network requests in your .NET project or application.

How to Filter out Undesired Network Requests

Here is an example of how you can use the NetworkDisabledMessageHandler class to filter out undesired network requests using the Aspose.HTML library:

  1. Create an instance of the Configuration class using Configuration() constructor.
  2. Call the GetService<INetworkService>() method on the configuration object to retrieve an instance of the INetworkService interface.
  3. Use the Insert() method to add the message handler to the top of existing message handler chain. This means that any network requests made through INetworkService will first pass through NetworkDisabledMessageHandler.
  4. Create an instance of the HTMLDocument class, passing to it the path to the HTML file and the configuration object.
 1// Create an instance of the Configuration class
 2using var configuration = new Configuration();
 3
 4// Call the INetworkService which contains the functionality for managing network operations
 5var network = configuration.GetService<INetworkService>();
 6
 7// Add the TimeoutMessageHandler to the top of existing message handler chain
 8network.MessageHandlers.Insert(0, new NetworkDisabledMessageHandler());                
 9
10// Prepare path to a source document file
11string documentPath = Path.Combine(DataDir, "document.html");
12
13// Create an HTML document with a custom configuration
14using var document = new HTMLDocument(documentPath, configuration);

You can download the complete examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.