How to capture all request with 'http' & 'https' scheme name?

1,039 views
Skip to first unread message

Christian LeMoussel

unread,
Mar 29, 2015, 3:03:52 AM3/29/15
to cefs...@googlegroups.com
To capture/modify requests/responses, It' necessary to register custom scheme handler.
I've implemented my SchemeHandler and SchemeHandlerFactory and registered that scheme in settings:

The problem is that only requests with 'hhtp' scheme are capturing. How to capture all request with 'http' & 'https' scheme name?



   
internal class CefSharpSchemeHandlerFactory : ISchemeHandlerFactory
   
{
       
public const string SchemeName = "http";

       
public ISchemeHandler Create()
       
{
         
return new CefSharpSchemeHandler();
       
}
   
}


    internal class CefSharpSchemeHandler : ISchemeHandler
   
{
       
public bool ProcessRequestAsync(IRequest request, ISchemeHandlerResponse response, OnRequestCompletedHandler requestCompletedCallback)
       
{
           
// TODO some stuff ......


    // Return false to execute the default behaviour
           
return false;
       
}
   
}

   
private static void BrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
   
{
       
if (e.IsMainFrame)
       
{
            browser
.FrameLoadEnd -= BrowserFrameLoadEnd;
       
}
   
}


   
static void Main(string[] args)
   
{
       
const string testUrl1 = "http://httpbin.org/";
 
const string testUrl2 = "https://httpbin.org/";

        CefSettings set = new CefSettings();
        set.RegisterScheme(new CefCustomScheme()
       
{
           
SchemeName = CefSharpSchemeHandlerFactory.schemeName,
           
SchemeHandlerFactory = new CefSharpSchemeHandlerFactory()
       
});
        Cef.Initialize(set);

        browser
= new ChromiumWebBrowser();
browser.FrameLoadEnd += BrowserFrameLoadEnd;
        browser
.Load(testUrl1);

       
Console.ReadKey();

       
Cef.Shutdown();
   
}


Alex Maitland

unread,
Mar 29, 2015, 5:08:41 AM3/29/15
to cefs...@googlegroups.com
You've only registered for `http`, you need to register twice, once for `http` and the second time for `https`. They are two different schemes.

Also `return false` for a `Scheme` handler will cancel the request. There is no default behavior, you are responsible for handling the entire request.

To intercept, you need to look at `IResourceHandlerFactory` e.g. https://github.com/cefsharp/CefSharp/blob/master/CefSharp/DefaultResourceHandlerFactory.cs

Alex Maitland

unread,
Mar 29, 2015, 5:24:47 AM3/29/15
to cefs...@googlegroups.com
Downside of `ResourceHandler` approach, is that it's only `sync`, at some point there will be an `async` implementation.

You can register a `Scheme` for a specific domain if you only wish to respond to specific requests.
Reply all
Reply to author
Forward
0 new messages