DotNetBrowser provides functionality that allows handling network activity including HTTP requests/responses. You can use the NetworkDelegate to handle all network activity of the Browser instances associated with specified BrowserContext.


With NetworkDelegate you can intercept all HTTP requests/responses headers and obtain information about each request/response stage. Below is the list of all request/response stages:


OnBeforeURLRequest


Fires when a request is about to occur. This event is sent before any TCP connection is made and can be used to redirect requests to another location. It can be used to access and modify POST data of the request when method type is "POST". See example.


OnBeforeSendHeaders


Fires when a request is about to occur and the initial headers have been prepared. It allows adding, modifying, and deleting HTTP request headers.


OnBeforeSendProxyHeaders


Fires after OnBeforeSendHeaders when a proxy connection is used. Provides information about the proxy connection, and allows adding, modifying, and deleting HTTP request headers.


OnSendHeaders


Fires right before the HTTP headers are sent to the network. This event is informational and it does not allow modifying HTTP headers.


OnHeadersReceived


Fires each time that an HTTP(S) response header is received. Due to redirects and authentication requests, this can happen multiple times per request. This event is intended to allow adding, modifying and deleting HTTP response headers, such as incoming Set-Cookie headers.


OnAuthRequired


Fires when a request receives an authentication challenge and is unable to respond using cached credentials. You can use this method to handle "basic" or "digest" authentication.


OnBeforeRedirect


Fires when a request is about to occur and the initial headers have been prepared. It allows adding, modifying, and deleting HTTP request headers.


OnResponseStarted


Fires when the first byte of the response body is received. For HTTP requests, this means that the status line and response headers are available. This event is informational.


OnDataReceived


Fires each time when a part of the response body is received. When the whole response body is received, OnCompleted will be fired. This event is informational.


OnCompleted


Fires when a request has been processed successfully or failed.


OnDestroyed


Fires when a request is being destroyed.


OnCanSetCookies


Fires when the engine is about to decide whether specified cookies can be set or not.


OnCanGetCookies


Fires when the engine is about to decide whether specified cookies can be received and send to a web server.


The following sample demonstrates how to change target URL using OnBeforeURLRequest event and print User-Agent HTTP header value when a user loads google.com:


MainWindow.xaml

<Window x:Class="WPF.NetworkEvents.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" >
    <Grid Name="mainLayout">
        
    </Grid>
</Window>

 


 

MainWindow.xaml.cs

using System;
using System.IO;
using DotNetBrowser;
using DotNetBrowser.WPF;
using System.Windows;

namespace WPF.NetworkEvents
{
    public partial class MainWindow : Window
    {
        Browser browser;
        WPFBrowserView browserView;

        public MainWindow()
        {
            InitializeComponent();
            BrowserContext browserContext = BrowserContext.DefaultContext;
            browserContext.NetworkService.NetworkDelegate = new SampleNetworkDelegate();

            browser = BrowserFactory.Create(browserContext);
            browserView = new WPFBrowserView(browser);
            mainLayout.Children.Add(browserView);
            browserView.Browser.LoadURL("http://www.teamdev.com");
        }
    }


    class SampleNetworkDelegate : DefaultNetworkDelegate {
   
        public override void OnBeforeURLRequest(BeforeURLRequestParams parameters) {
            // If navigate to teamdev.com, then change URL to google.com.
            if (parameters.Url.Equals("http://www.teamdev.com/")) {
                parameters.SetUrl("www.google.com");
            }
        }


        public override void OnBeforeSendHeaders(BeforeSendHeadersParams parameters) {
            // If navigate to google.com, then print User-Agent header value.
            if (parameters.Url.Equals("http://www.google.com/")) {
                HttpHeaders headers = parameters.Headers;
                Console.WriteLine("User-Agent: " + headers.GetHeader("User-Agent"));
            }
        }
    }
}