Page 1 of 1

Get request API

PostPosted: Mon Nov 09, 2020 2:49 pm
by dankoe
Hello, is it possible to create an get request with CefSharp?

The aim is to make an get request to a server, the response contains a requested webpage which should be displayed with CefSharp ChromiumWebBrowser element. I will be thankful for every codesnippet / tipps.

Kind regards

Re: Get request API

PostPosted: Mon Nov 09, 2020 4:28 pm
by amaitland

Re: Get request API

PostPosted: Tue Nov 10, 2020 7:58 am
by dankoe
Thanks for your reply!

Now, I have an working GET request but cannot use it because javascript seems to be disabled.

Code: Select all

var frame = browser.GetMainFrame();

            //Create a new request
            var request = frame.CreateRequest(initializePostData: false);
            request.Method = "GET";       
            request.Url = "https://myUrl/?auth=KIS";

            NameValueCollection headers = new NameValueCollection();
            headers.Add("username", "ericm");
            headers.Add("accesstoken", LoadCustomRequestExample());
            request.Headers = headers;

I am getting the error message:
Code: Select all
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/favicon.ico">..................<strong>We're sorry but xxxx-vuejs doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div><script src="/js/app.d18ae00f.js"></script></body></html>

Is it possible to use javascript with cefsharp? Thanks in advance!

Re: Get request API

PostPosted: Tue Nov 10, 2020 2:01 pm
by amaitland
JavaScript is enabled by default.

Re: Get request API

PostPosted: Tue Nov 10, 2020 3:12 pm
by dankoe
Thanks for your reply! I've found my problem. There is an error with the get request.

I don't know why, but the header isn't set correctly. The username and access-token is missing in chrome network tab. This causes in an internal server err.
I am using CefSharp v.85. Am I doing something wrong? Thanks in advance!

Re: Get request API

PostPosted: Tue Nov 10, 2020 7:26 pm
by amaitland
Try calling Load instead of LoadRequest and modifying the Request as shown in ... h-postdata to add your headers.

LoadRequest can be a little problematic.

Re: Get request API

PostPosted: Wed Nov 11, 2020 3:21 am
by dankoe
Hey, thanks for your suggestion. I've tried but it doesn't work - the header is still missing. Am I doing something wrong? Its like suggested, or isn't? Thank you very much for your opinion

Code: Select all
public void InitBrowser()
            var settings = new CefSettings();

            //For legacy biding we'll still have support for
            //CefSharpSettings.LegacyJavascriptBindingEnabled = true;
            //browser = new ChromiumWebBrowser("https://myURL/login");
            //browser.Dock = DockStyle.Fill;
            browser = new ChromiumWebBrowser("https://myURL/calendar/?auth=KIS");
            browser.IsBrowserInitializedChanged += (se, ev) =>
            browser.RequestHandler = new CustomRequestHandler();

            browser.Dock = DockStyle.Fill;


        public class CustomResourceRequestHandler : CefSharp.Handler.ResourceRequestHandler
            protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
                //Get URL
                //Modify the request to add post data
                var postData = new PostData();

                request.Method = "GET";
                //request.PostData = postData;

                //Set the Content-Type header to whatever suites your requirement
                request.SetHeaderByName("username", "support", true);
                request.SetHeaderByName("access-token", GetToken(), true);
                return CefReturnValue.Continue;

            protected string GetToken() {

                // --------------------------------------------
                //Get Login Token
                var client = new RestClient("http://myURL/smaug");
                client.Timeout = -1;
                var requestPOST = new RestRequest(Method.POST);
                requestPOST.AddHeader("Content-Type", "application/x-www-form-urlencoded");
                requestPOST.AddParameter("client", "Rm9....");
                requestPOST.AddParameter("password", "...12358");
                requestPOST.AddParameter("request-type", "authentication");
                requestPOST.AddParameter("username", "support");
                IRestResponse response = client.Execute(requestPOST);

                // parse the json response so that we can get at the key/value pairs
                string token = "";
                if (response.Content.Length > 0)
                    dynamic api = JObject.Parse(response.Content);
                    // grab the values
                    token = api.accessToken;
                return token;

        public class CustomRequestHandler : CefSharp.Handler.RequestHandler
            protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)

                //see the get data
                if (request.Url == "https://myURL/calendar/?auth=KIS")
                    return new CustomResourceRequestHandler();

                //Default behaviour, url will be loaded normally.
                return null;