Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To filter network requests in C#, create a custom MessageHandler, inspect RequestMessage.RequestUri.Protocol, and allow only the protocols your application trusts before passing the request through the Aspose.HTML for .NET pipeline.
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. HTML processing can trigger network requests when a document references external images, stylesheets, scripts, fonts, or other resources.
Message handlers are a useful way to intercept and process these requests in a pipeline. This article shows how to create a custom handler that checks request protocols and blocks unwanted network requests before the document is fully loaded.
Aspose.HTML for .NET provides functionality that helps when you need to restrict resource loading in an HTML processing application. For example, a server-side conversion workflow may need to load local files and inline resources but block external HTTP or HTTPS requests.
The example creates a NetworkDisabledMessageHandler that allows requests with the "file:", "about:", or "base64:" protocols. If the request uses another protocol, the handler blocks it.
To create the filtering handler:
NetworkDisabledMessageHandler class derived from
MessageHandler.RequestMessage. 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 handler customizes the behavior of network requests based on their protocols. You can adapt the logic inside Invoke() for your own allowlist. In this example, requests continue only when the protocol is "file:", "base64:", or "about:"; other requests are blocked by the handler.
This custom message handler can filter out network requests in your .NET project or application.
After the handler is implemented, add it to the network service used by the document configuration:
Insert() to add the message handler to the top of the existing message handler chain. This means network requests made through INetworkService pass through NetworkDisabledMessageHandler first. 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.
Filtering helps prevent unwanted external resources from being loaded when an HTML document is opened, converted, rendered, or processed in a controlled environment.
The example allows "file:", "about:", and "base64:" requests. You can change the allowlist inside Invoke() to match your application policy.
Placing the handler first lets it inspect and block requests before later handlers or default network processing can load the resource.
MessageHandler, override Invoke(), and add a handler to the pipeline.Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.