How to add header in each request?

1,519 views
Skip to first unread message

Amogh Shah

unread,
Jul 8, 2014, 5:39:28 AM7/8/14
to cefs...@googlegroups.com

I successfully implemented CEF version 31 in my C# application. Now what I want is, I want to add an request header in each request. I have checked the method OnBeforeBrowse(IWebBrowser browser, IRequest request, NavigationType naigationvType, bool isRedirect) but I cannot change Headers of IRequest request object.

Is their any way to do this?

Rich McNeary

unread,
Jul 8, 2014, 9:24:34 AM7/8/14
to cefs...@googlegroups.com
Serendipitous. There is a recent pull request for CefSharp3 to deal with this (pull request 413). Refresh from the master branch then use OnBeforeResourceLoad() like this:

        public bool OnBeforeResourceLoad (IWebBrowser browser, IRequestResponse requestResponse)
        {
            // The collection returned here is a copy of the collection held by
            // CEF, changing this collection will have no effect until the
            // Request's Headers property is set.
            var headers = requestResponse.Request.Headers;

            headers["User-Agent"] = "My User Agent";

            // Must set the headers back on the property, we get a copy of the
            // headers above not a reference.
            requestResponse.Request.Headers = headers;

            // False means continue.
            return false;
        }
Reply all
Reply to author
Forward
0 new messages