Since DotNetBrowser 1.13 it is possible to register custom protocol handlers that will be used to intercept and handle all the URL requests for both standard URL schemes (such as http, https, ftp, file) and custom schemes declared in your application.

After registering a protocol handler for a scheme, all the URLs with the specified scheme loaded into the Browser will be handled by this handler.


Example


using System;
using System.Text;
using System.Threading;
using DotNetBrowser;
using DotNetBrowser.Protocols;

namespace ProtocolServiceTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (Browser browser = BrowserFactory.Create())
            {
                //Event for detecting if the page is loaded
                ManualResetEvent loadedEvent = new ManualResetEvent(false);

                browser.FinishLoadingFrameEvent += (finishSender, finishArgs) =>
                {
                    if (finishArgs.IsMainFrame)
                    {
                        Console.WriteLine(browser.GetHTML());
                        loadedEvent.Set();
                    }
                };

                //Registering the handler for the specified protocol
                browser.Context.ProtocolService.Register("https", new HttpsHandler());

                //Loading Url with the same protocol as registered
                browser.LoadURL("https://request.url");

                //Waiting the page loading
                loadedEvent.WaitOne();
            }

            Console.ReadKey();
        }
    }


    //The instance of this type will handle the requests of the specified protocol
    public class HttpsHandler : IProtocolHandler
    {
        //This method should provide the response for the specified request
        public IUrlResponse Handle(IUrlRequest request)
        {
            string htmlContent = "Request Url: " + request.Url + "\n";
            return new UrlResponse(Encoding.UTF8.GetBytes(htmlContent));
        }
    }
}