My Account |  Site Map | Contact Us  
 The same content in Microsoft HTML help file format is included in the download package.
Using Custom Resource Handler

A resource handler allows you to intercept a Web request and serve the contents with your own code. For example, the TabbedBrowser sample allow you enter an Url "sample://embedded_page", this Url is then intercepted by SampleHandler.ProcessRequest method. This method can then dynamically serve this page. Follow these steps to use resource handler:

  1. Register none standard protocols schemes

    If your custom Url uses a none-standard scheme, then you must register it with EO.WebBrowser first. For example, if you wish to intercept "http://www.yoursite.com/about" and handle it with your code, then the protocol scheme is "http". Since "http" is a standard scheme, so it is not necessary for you to register it. However, if you wish to handle "sample://embedded_page", then the protocol scheme is "sample", which is not a standard protocol scheme. In this case you must register the scheme by calling EO.WebBrowser.Runtime.RegisterCustomSchemes. The following code registers two custom schemes "sample" and "test":

    //Register custom scheme "sample" and "test"
    EO.WebBrowser.Runtime.RegisterCustomSchemes("sample", "test");
  2. Implement your ResourceHandler class.

    ResourceHandler is an abstract class with two methods that you must implement:

    • Match. This method is called for every request and the current request is passed to the function as a Request object. Inside the function you can examine the Request object and return true from this function to indicate that your custom resource handler wish to handle this request. The following code in the sample project simply checks whether the request Url starts with our custom scheme:

      internal class SampleHandler : ResourceHandler
          public const string SampleUrlPrefix = "sample://";
          public override bool Match(Request request)
              //Return true if the request Url matches our scheme. In that
              //case ProcessRequest will be called to handle the request
              return request.Url.StartsWith(SampleUrlPrefix, StringComparison.OrdinalIgnoreCase);
    • ProcessRequest. This method is called for a Request if Match has returned true for that request. Furture more, an additional Response object is passed this function with which you can send the response data. For example, the following code sends the contents of EmbeddedPage.htm to the client:

      internal class SampleHandler : ResourceHandler
          public const string EmbeddedPageUrl = "sample://embedded_page";
          public override void ProcessRequest(Request request, Response response)
              //Only process EmbeddedPageUrl
              if (string.Compare(request.Url, EmbeddedPageUrl, true) == 0)
                  //Set content type
                  response.ContentType = "text/html";
                  //Copy contents of EmbeddedPage.htm to the output stream
                  byte[] buffer = new byte[1024];
                  Stream stream = typeof(SampleHandler).Assembly.GetManifestResourceStream("EO.TabbedBrowser.EmbeddedPage.htm");
                  while (true)
                      int nBytesRead = stream.Read(buffer, 0, buffer.Length);
                      if (nBytesRead <= 0)
                      response.OutputStream.Write(buffer, 0, nBytesRead);

      It is important to remember that:

      • ProcessRequest is called by a worker thread instead of the main UI thread;
      • There can be multiple thread calling ProcessRequest for different Request object at the same time;

      As such, you must properly handle synchronization issues in your ProcessRequest implementation.

  3. Register your ResourceHandler with WebView

    Call WebView.RegisterResourceHandler to register your resource handler with the WebView object. For example:

    //Register SampleHandler with the WebView
    webView1.RegisterResourceHandler(new SampleHandler());

After the above steps, sample Url "sample://embedded_page" will be handled by SampleHandler class. You can see this feature in action by starting TabbedBrowser sample, then select "Embedded Page Sample" from the menu.

Direct link to this topic