Filter Network Requests in C#

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.

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. 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.

Message Handler to Filter Network Requests

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:

  1. Use the Aspose.Html.Net namespace.
  2. Define a custom NetworkDisabledMessageHandler class derived from MessageHandler.
  3. Override Invoke() to inspect each request.
  4. Read RequestUri from the RequestMessage.
  5. Check the Protocol value and allow only trusted protocols.
 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.

How to Filter out Undesired Network Requests

After the handler is implemented, add it to the network service used by the document configuration:

  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 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.
  4. Create an instance of the HTMLDocument class, passing to it the path to the HTML file and the configuration object.
 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.

FAQ

Why filter network requests during HTML processing?

Filtering helps prevent unwanted external resources from being loaded when an HTML document is opened, converted, rendered, or processed in a controlled environment.

Which protocols does the example allow?

The example allows "file:", "about:", and "base64:" requests. You can change the allowlist inside Invoke() to match your application policy.

Why insert the handler at the top of the pipeline?

Placing the handler first lets it inspect and block requests before later handlers or default network processing can load the resource.

Related Articles