Get request API

Having problems with building or using the CefSharp .NET binding? Ask your CEF-related questions here. Please ask general usage questions on StackOverflow.

Moderator: amaitland

Get request API

Postby dankoe » Mon Nov 09, 2020 2:49 pm

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
dankoe
Newbie
 
Posts: 4
Joined: Mon Nov 09, 2020 2:38 pm

Re: Get request API

Postby amaitland » Mon Nov 09, 2020 4:28 pm

Maintainer of the CefSharp project.
amaitland
Master
 
Posts: 965
Joined: Wed Jan 14, 2015 2:35 am

Re: Get request API

Postby dankoe » Tue Nov 10, 2020 7:58 am

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";

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



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!
dankoe
Newbie
 
Posts: 4
Joined: Mon Nov 09, 2020 2:38 pm

Re: Get request API

Postby amaitland » Tue Nov 10, 2020 2:01 pm

JavaScript is enabled by default.
Maintainer of the CefSharp project.
amaitland
Master
 
Posts: 965
Joined: Wed Jan 14, 2015 2:35 am

Re: Get request API

Postby dankoe » Tue Nov 10, 2020 3:12 pm

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!
Attachments
CefSharp.png
my get request and chrome network tab
CefSharp.png (89.38 KiB) Viewed 1595 times
dankoe
Newbie
 
Posts: 4
Joined: Mon Nov 09, 2020 2:38 pm

Re: Get request API

Postby amaitland » Tue Nov 10, 2020 7:26 pm

Try calling Load instead of LoadRequest and modifying the Request as shown in https://github.com/cefsharp/CefSharp/wi ... h-postdata to add your headers.

LoadRequest can be a little problematic.
Maintainer of the CefSharp project.
amaitland
Master
 
Posts: 965
Joined: Wed Jan 14, 2015 2:35 am

Re: Get request API

Postby dankoe » Wed Nov 11, 2020 3:21 am

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();
            Cef.Initialize(settings);

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

            this.Controls.Add(browser);
            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;
            }
        }
dankoe
Newbie
 
Posts: 4
Joined: Mon Nov 09, 2020 2:38 pm


Return to CefSharp Forum

Who is online

Users browsing this forum: No registered users and 3 guests