Reputation: 169
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.
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.
Upvotes: 2
Views: 5524
Reputation: 169
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);
Upvotes: 7
Reputation: 4420
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
Upvotes: 2