Page 1 of 2

LoadRequest in Cef simple apllication

PostPosted: Sat Oct 02, 2021 5:41 pm
by chayal
Hi ,
I am new to CEF3. I want to load a simple request using loadRequest in CEF simple application.
I saw that in cef client project there is RunRequesTest API to call loadRequest but thats just dummy request.
Can you please help me with sample code as to how and where to call LoadRequest.

Re: LoadRequest in Cef simple apllication

PostPosted: Sun Oct 03, 2021 3:00 am
by magreenblatt
You can call LoadRequest at any time after a page from the same origin has been loaded (for example, from OnLoadEnd). See https://bitbucket.org/chromiumembedded/ ... m-requests

Re: LoadRequest in Cef simple apllication

PostPosted: Sun Oct 03, 2021 3:54 am
by chayal
Does it has to be called on the same frame on which LoadURL has been called.
Also is there any sample testcase for this..I know of cefsimple app. Is there any running example of loadrequest

Re: LoadRequest in Cef simple apllication

PostPosted: Sun Oct 03, 2021 4:34 am
by magreenblatt
It can be called on any frame with the same origin. It sounds like you’ve found the examples.

Re: LoadRequest in Cef simple apllication

PostPosted: Mon Oct 04, 2021 11:21 am
by chayal
Hi magreenblatt
I tried to load my custom request in test_runner.cc and ran the "request" from cefclient . But no request is sent from the application. I further confirmed it using fiddler. Can you suggest me if there is anything wrong with loadrequest in CEF3.
Also is there any alternate of using loadrequest. My aim is basically to load a request which has some headers as well.

Re: LoadRequest in Cef simple apllication

PostPosted: Mon Oct 04, 2021 12:04 pm
by magreenblatt
Show code. Where are you calling LoadRequest? What URL is loaded currently?

Re: LoadRequest in Cef simple apllication

PostPosted: Tue Oct 05, 2021 6:54 am
by chayal
I am trying to call loadrequest from the cefsimple application. I follow following steps:
1. Load a URL from SimpleApp::OnContextInitialized().
2. Implement OnLoadEnd inSimpleHelper.
3. call LoadRequest from OnLoadEnd()
In file SimpleHandler.cc:
I create a simple GET request in function CreateGETRequest :
CefRefPtr<CefRequest>SimpleHandler::CreateGETRequest()
{
CefRefPtr<CefRequest> request(CefRequest::Create());
request->SetURL("https://www.facebook.com");
request->SetMethod(CefString("GET"));

return request;
}

And implement OnLoadEnd ad follows:
void SimpleHandler::OnLoadEnd(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
int httpStatusCode)
{
CefRefPtr<CefRequest> request = CreateGETRequest();
//CefRefPtr<MyRequestClient> client = new MyRequestClient();
//CefRefPtr<CefURLRequest> url_request = CefURLRequest::Create(request, client.get(), nullptr);
browser->GetMainFrame()->LoadRequest(request);
}


If I try to call the same request using CefURLRequestClient method, I am able to send my request and verified it using Fiddler.
I am using cef_binary_92.0.27+g274abcf+chromium-92.0.4515.159_windows32.tar

Re: LoadRequest in Cef simple apllication

PostPosted: Tue Oct 05, 2021 7:20 am
by magreenblatt
You need to navigate the browser to https://www.facebook.com (same origin as the request) before you can call LoadRequest. Since you're using a basic GET request, and you're already on the same page, it's probably having no visible effect.

Re: LoadRequest in Cef simple apllication

PostPosted: Thu Oct 07, 2021 5:13 pm
by chayal
Hi magreenblatt,
Thanks for the prompt response.
I have some questions though:
Lets take example of hostname as abc.com and port 8080. So I called APIs as follows:
LoadURL("http://www.abc.com:8080")
I created the POST request from the above URL and called LoadRequest on it. I was able to load the post request successfully.

Ques 1: Can't we use "about:blank" to load URL and then call the LoadRequest on the above server. I did try this but did not get the result . So just confirming.

Ques 2: If we can't use "about:blank" , then I have to first load URL to the server for which I get an intermediate response and a webpage is loaded before I could I get the actual LoadRequest response. My problem is that I don't want to display that intermediate webpage. What should I do to directly display request response.

Thanks in advance :)

Re: LoadRequest in Cef simple apllication

PostPosted: Thu Oct 07, 2021 5:50 pm
by amaitland
about:blank is a special case, from memory chromium doesn't actually render a html page, it simply displays a blank page.

You can add post data to a request in CefResourceRequestHandler::OnBeforeResourceLoad, simply call LoadUrl as normal and using CefRequestHandler/CefResourceRequestHandler You can modify the request.

https://github.com/cefsharp/CefSharp/wi ... h-postdata has a CefSharp specific example written in c# which should give you a rough guide.