Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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.
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.
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// Disable external network access while allowing local and embedded resources
2
3// Define the NetworkDisabledMessageHandler class that is derived from the MessageHandler class
4public class NetworkDisabledMessageHandler : MessageHandler
5{
6 // Override the Invoke() method
7 public override void Invoke(INetworkOperationContext context)
8 {
9 if (context.Request.RequestUri.Protocol == "file:" ||
10 context.Request.RequestUri.Protocol == "base64:" ||
11 context.Request.RequestUri.Protocol == "about:")
12 Next(context);
13 }
14}The above C# code snippet creates a message handler to customize the behavior of network requests based on their protocols:
NetworkDisabledMessageHandler class. This class inherits from the
MessageHandler class and overrides its
Invoke() method to handle network operations.This custom message handler can filter out network requests in your .NET project or application.
Here is an example of how you can use the NetworkDisabledMessageHandler class to filter out undesired network requests using the Aspose.HTML library:
NetworkDisabledMessageHandler. 1// Disable network requests when loading HTML
2
3// Create an instance of the Configuration class
4using Configuration configuration = new Configuration();
5
6// Call the INetworkService which contains the functionality for managing network operations
7INetworkService network = configuration.GetService<INetworkService>();
8
9// Add the NetworkDisabledMessageHandler to the top of existing message handler chain
10network.MessageHandlers.Insert(0, new NetworkDisabledMessageHandler());
11
12// Prepare path to a source document file
13string documentPath = Path.Combine(DataDir, "document.html");
14
15// Create an HTML document with a custom configuration
16using HTMLDocument document = new HTMLDocument(documentPath, configuration);You can download the complete examples and data files from GitHub.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.