Search code examples
c#chromium-embeddedcefsharp

CEFSharp - Read web responses


I am working on a project that I want to use the Chromium Web Browser and be able to read the data that would normally come though the DevTools "Network" tab. So basically all i really need is the URL and the status code (404, 200, 50x, etc).

I have everything working properly for the ChromiumWebBrowser part because that works perfectly but I cant seem to figure out the details on how to get the network data.

I found this in a github issues section but I dont really understand how to implement it. https://github.com/cefsharp/CefSharp/issues/1379

Any help would be greatly appreciated.

Here is what I have so far...

private ChromiumWebBrowser _wb;

    public MainForm()
    {
        var cefsettings = new CefSettings { CachePath = "cache" };
        cefsettings.CachePath = "cache";
        if (cefsettings.CefCommandLineArgs.ContainsKey("enable-system-flash"))
        {
            string flashValue;
            cefsettings.CefCommandLineArgs.TryGetValue("enable-system-flash", out flashValue);
            if (flashValue != "1")
            {
                Debug.WriteLine("Flash Might Be Disabled For Chromium Web Browser");
            }
        }
        else
        {
            cefsettings.CefCommandLineArgs.Add("enable-system-flash", "1");
        }
        //TODO: Get the latest version version folder
        cefsettings.CefCommandLineArgs.Add("ppapi-flash-path","C:\\program Files (x86)\\Google\\Chrome\\Application\\51.0.2704.103\\PepperFlash\\pepflashplayer.dll");
        Cef.Initialize(cefsettings);

        InitializeComponent();

        _wb = new ChromiumWebBrowser("http://youtube.com/")
        {
            Dock = DockStyle.Fill,
            Location = new System.Drawing.Point(0, 22),
            MinimumSize = new System.Drawing.Size(20, 20),
            Size = new System.Drawing.Size(1280, 900),
            TabIndex = 8
        };

        //Add ChromiumWebBrowser to the Browser Panel
        pnlBrowser.Controls.Add(_wb);
    }

Solution

  • Here is what I ended up doing...

    Implemented a class called "RequestHandler" that implements the IRequestHandler interface. Copied most of the default code for this interfaces methods from the CEFSharp open source project and then just tweaked the "IRequestHandler.OnResourceResponse" portion to my liking.

    Then on my main form that uses the web browser, I just used the code below...

    //Create ChromiumWebBrowser
    _wb = new ChromiumWebBrowser(Urls.HOME)
    {
        Dock = DockStyle.Fill,
        Location = new System.Drawing.Point(0, 22),
        MinimumSize = new System.Drawing.Size(20, 20),
        Size = new System.Drawing.Size(1280, 900),
        TabIndex = 8
    };
    
    //Add ChromiumWebBrowser to the Browser Panel and add events
    pnlBrowser.Controls.Add(_wb);
    var requestHandler = new RequestHandler();
    _wb.RequestHandler = requestHandler;
    

    I hope this helps someone!