Note: Advice in this article will only work for DotNetBrowser 1.
See the corresponding article for DotNetBrowser 2 here.
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
C#
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)); } } }
VB.NET
Imports System.Text Imports System.Threading Imports DotNetBrowser Imports DotNetBrowser.Protocols Module ProtocolServiceTest Sub Main(ByVal args As String()) Using browser As Browser = BrowserFactory.Create() Dim loadedEvent As ManualResetEvent = New ManualResetEvent(False) AddHandler browser.FinishLoadingFrameEvent, sub(finishSender, finishArgs) If finishArgs.IsMainFrame Then Console.WriteLine(browser.GetHTML()) loadedEvent.Set() End If End sub browser.Context.ProtocolService.Register("https", New HttpsHandler()) browser.LoadURL("https://request.url") loadedEvent.WaitOne() End Using Console.ReadKey() End Sub Public Class HttpsHandler Implements IProtocolHandler Public Function Handle(ByVal request As IUrlRequest) As IUrlResponse _ Implements IProtocolHandler.Handle Dim htmlContent As String = "Request Url: " & request.Url & vbLf Return New UrlResponse(Encoding.UTF8.GetBytes(htmlContent)) End Function End Class End Module