1

I'm trying to change the user agent dynamically using CefSharp. I have looked at the IRequestHandler class and I no longer see a method called OnBeforeResourceLoad.

I did, however, find a method called OnBeforeBrowse. However, all of the request headers are empty and when I try and add one, it simply does not add. I have tried to take a reference, change the value and re-assign, but no dice.

  • CefSharp: 75.1.142
  • .NET: 4.7.2
  • OffScreen

I found a method called OnBeforeBrowse in IRequestHandler. However, all of the request headers are empty and when I try and add one via the IRequest param, it simply does not add. I have tried to take a reference, change the value and re-assign, but no dice. Always comes back empty with no keys.

protected override bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
{
    var headers = request.Headers;
    headers["User-Agent"] = "New user agent";
    request.Headers = headers;
    return false;
}

I expect the user agent to change to the value I give it.

Pang
  • 9,564
  • 146
  • 81
  • 122
Ginko
  • 149
  • 2
  • 12
  • As per the release notes you need to review https://github.com/cefsharp/CefSharp/issues/2743 for a list of changes – amaitland Oct 02 '19 at 07:27
  • Thank you for the link. I searched the change log and Google already, but it didnt bring any hits. How do I attach the ResourceRequestHandler to the ChromiumWebBrowser? – Ginko Oct 02 '19 at 08:27
  • http://cefsharp.github.io/api/75.1.x/html/M_CefSharp_IRequestHandler_GetResourceRequestHandler.htm The examples in the project source have been updated in you need a reference. – amaitland Oct 02 '19 at 09:16
  • I was typing up a reply and didnt see this until now. I will take a look tomorrow. Its been a long day of CefSharp and I need a longggg rest. Thank you for the new project files – Ginko Oct 02 '19 at 09:48

2 Answers2

6

I have spent a lot of time on this and I couldn't find any examples in light of these new changes to the CefSharp library, so I have put together something that works. I used bits and pieces I found online and put it all together. It may not be perfect, but I couldnt find a straight forward answer anywhere!

Define the class which implements ResourceRequestHandler. The base class has the required OnBeforeResourceLoad function. I added a userAgent string to the constructor see it can be passed from calling fucntions.

    public class ResourceRequestHandlerExt : ResourceRequestHandler
    {
        private string userAgent;

        public ResourceRequestHandlerExt(string userAgent)
        {
            this.userAgent = userAgent;
        }

        protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
        {
            var headers = request.Headers;
            headers["User-Agent"] = userAgent;
            request.Headers = headers;

            return base.OnBeforeResourceLoad(chromiumWebBrowser, browser, frame, request, callback);
        }
    }

Define the class which implements RequestHandler. The base class has a required GetResourceRequestHandler function which allows use to pass our user agent to the ResourceRequestHandlerExt class.

    public class RequestHandlerExt : RequestHandler
    {
        private string userAgent;

        public RequestHandlerExt(string userAgent)
        {
            this.userAgent = userAgent;
        }

        protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
        {
            if (!string.IsNullOrEmpty(userAgent)) return new ResourceRequestHandlerExt(userAgent);
            else return base.GetResourceRequestHandler(chromiumWebBrowser, browser, frame, request, isNavigation, isDownload, requestInitiator, ref disableDefaultHandling);
        }
    }

When instantiating the ChromiumWebBrowser object, you set the RequestHandler to the RequestHandlerExt class above using:

ChromiumWebBrowser browser = new ChromiumWebBrowser();
browser.RequestHandler = new RequestHandlerExt(userAgent);
  • Specifying a user agent in CefSettings will get overwritten, so its not needed in this case.
  • If you do not specify a user agent, then no headers will be added/modified
  • The user agent can be changed for each browser.Load(url) call.
Ginko
  • 149
  • 2
  • 12
  • 1
    There are examples in the source https://github.com/cefsharp/CefSharp/blob/cefsharp/75/CefSharp.Example/Handlers/ExampleRequestHandler.cs https://github.com/cefsharp/CefSharp/blob/cefsharp/75/CefSharp.Example/Handlers/ExampleResourceRequestHandler.cs – amaitland Oct 02 '19 at 10:25
  • It's not very user friendly to dive into the source of every library I try and use. I just took a look at the docs I found via Google, but they reference the old examples and old ways of doing things, otherwise, they would have been great. Maybe I'm not as competent or something, but I just want a working example. I have provided one now, so its done. – Ginko Oct 02 '19 at 22:11
  • 1
    It's an open source project, that's one of the first place you should look in my opinion. – amaitland Oct 02 '19 at 23:15
  • Can you provide the class details `ResourceRequestHandler and RequestHandler`, I am not able to correctly implement this. – Neeraj Dubey Oct 09 '20 at 02:34
2

Starting with version 75 CEF now supports the Chromium Network Service which brings a huge number of breaking API changes.

As per https://github.com/cefsharp/CefSharp/issues/2743

Resource-related callbacks have been moved from IRequestHandler to a new IResourceRequestHandler interface which is returned via the IRequestHandler.GetResourceRequestHandler method

amaitland
  • 4,073
  • 3
  • 25
  • 63
  • 1
    See https://github.com/cefsharp/CefSharp/wiki/General-Usage#useragent for an example. – amaitland Sep 23 '20 at 22:56
  • As additional resources : https://github.com/cefsharp/CefSharp/blob/25be7265f20d4a4651cd607cabe4cd6797c77169/CefSharp.Example/RequestHandler.cs – Eyni Kave Jun 14 '22 at 13:21
  • 1
    @EyniKave That example is out of date and Includes methods that were removed. There are up to date examples available at https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Example/Handlers/ExampleRequestHandler.cs and https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Example/Handlers/ExampleResourceRequestHandler.cs – amaitland Jun 14 '22 at 20:42