Setting the User-Agent

2,671 views
Skip to first unread message

Daniel Cohee

unread,
May 26, 2012, 11:25:04 PM5/26/12
to cefs...@googlegroups.com
I want to append to the User-Agent string from my in-house c# winforms application.  Is there a simple way to do this?  Or would I need to modify the CefShap.Winforms project to expose access to this property?

anthony taranto

unread,
May 27, 2012, 1:30:01 AM5/27/12
to cefs...@googlegroups.com
Message has been deleted

Daniel Cohee

unread,
May 27, 2012, 6:18:44 PM5/27/12
to cefs...@googlegroups.com
Thanks, yeah, I did see that. I have been going through the CEF documentation and the CefSharp code to get familiar with it.  I am very new to both projects, so I may end up asking a stupid question.  

In CefSharp.WinForms.WebView, CEF is initialized with CEF::Initialize(gcnew Settings). After initialization, I was trying to find out (a) what the settings ended being based on default initialization, and (b) How to change them (specifically user-agent).

Perhaps I have this backwards, e.g., I have to create my own settings and initialize CEF with them.  But I was hoping I could read the existing settings, and modify them during run-time.  In which case, I believe I would modify the WebView example provided to pass in my CEF settings and initialize CEF with them.

anthony taranto

unread,
May 27, 2012, 6:34:29 PM5/27/12
to cefs...@googlegroups.com
Don't worry, there are no stupid questions, only stupid maintainers.

There are two settings classes. The first is 'Settings', which is passed to CEF.Initialize() and affects all browser instances. The second is `BrowserSettings`, which is passed when instantiating a WebView.

Both of these classes are somewhat crude in that CEF/CefSharp reads the values once, and then does not observe changes after the initial read. In other words, if you call CEF.Initialize() with settings.UserAgent equal to "Foo", then set settings.UserAgent to "Bar" after calling CEF.Initialize(), CEF will still use "Foo" as the user agent for all requests.

Long term, it would be good to add an API to allow modification of the settings after the initial read.

In the short term, if you need to change your UserAgent on the fly, you should implement IRequestHandler.OnBeforeResourceLoad(), which will allow you to modify the request headers.

https://github.com/ataranto/CefSharp/blob/master/CefSharp/IRequestHandler.h#L26

Daniel Cohee

unread,
May 27, 2012, 11:30:44 PM5/27/12
to cefs...@googlegroups.com
Thanks, that was helpful.  I think I am tracking now.

I tried implementing IRequestHandler and I am most probably doing something wrong as I can't seem to capture any of the OnBefore... events.

To implement it, all I did was add IRequestHandler to my form:

public partial class Form1 : Form, IRequestHandler

I then added the members:

bool IRequestHandler.OnBeforeBrowse(IWebBrowser browser, IRequest request, 
                           NavigationType naigationvType, bool isRedirect)
{
    System.Diagnostics.Debug.WriteLine("OnBeforeBrowse");
    return false;
}

bool IRequestHandler.OnBeforeResourceLoad(IWebBrowser browser, 
                                 IRequestResponse requestResponse)
{
    System.Diagnostics.Debug.WriteLine("OnBeforeResourceLoad");
    return false;
}

void IRequestHandler.OnResourceResponse(IWebBrowser browser, string url, 
                               int status, string statusText, 
                               string mimeType, WebHeaderCollection headers)
{
    System.Diagnostics.Debug.WriteLine("OnResourceResponse");
}

I never get any events, so I am obviously doing something wrong.  

The closest example I could find was in the CefSharp.Example project, the ExamplePresenter class attempts to do the same thing I am trying to do. The example project works perfect, but my project does not.  Perhaps there is something obvious I am doing wrong?

anthony taranto

unread,
May 27, 2012, 11:36:11 PM5/27/12
to cefs...@googlegroups.com
You'll have to tel CefSharp that Form1 will be your IRequestHandler implementation. So, from within a Form1 method:

var web_view = new WebView();
web_view.RequestHandler = this;

Daniel Cohee

unread,
May 27, 2012, 11:40:08 PM5/27/12
to cefs...@googlegroups.com
You seriously rock!!!   That did it!
Reply all
Reply to author
Forward
0 new messages