Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.

Problem with reading PosData in Request from Website

Post Reply
AuronTLG
Posts: 2
Joined: Mon Jan 06, 2020 3:39 pm

Problem with reading PosData in Request from Website

Post by AuronTLG »

Hello,

I've got the following problem:

I am currently trying to replace the Standard Delphi-TWebBrowser with TChromiumWindow in an application, that interacts with a website and as final step receives information in an xml-file from the website.

With the old TWebBrowser I simply used its BeforeNavigate2-Event, checked the URL and then processed PostData to access the xml-file.

While attempting to mirror that functionality with CEF4Delphi, I have currently run into a wall:
First I simply tried it by using the BeforeBrowse-Event to access the request with the right URL. However, both the HeaderMap as well as the PostData of the Request are always Nil.

Therefore I consulted various Demos like for example the "PostInspectorBrowser", "MiniBrowser" and "ResponseFilterBrowser" and tried to work with other events like "OnBeforeResourceLoad" etc, but so far to no avail.
As it is entirely possible that I made a very basic error considering that I am unfamiliar with the component, I first wanted to ask, what the right approach for my case would be.

Thanks.
User avatar
salvadordf
Posts: 4052
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Problem with reading PosData in Request from Website

Post by salvadordf »

Hi,

According to the CEF documentation the TChromium.OnBeforeResourceLoad event can be used to inspect or modify the request before the resource is loaded. You should be able to modify the POST request data and some of the HTTP headers using that event.

The XML file sent by servers is usually located in the response body.

If the XML file is loaded and the browser shows it the screen then you can call TChromium.RetrieveHTML and get the XML in the TChromium.TextResultAvailable event but if the XML is not visible in the screen then you have to use alternative methods.

The easiest way would be to use a TCEFUrlRequestClientComponent as shown in the URLRequest demo. This demo can send custom POST requests and receive all the data in the response.

If you can't use a URLRequest you can also use a response filter as shown in the ResponseFilterBrowser demo. This demo can intercept anything received from the server.
AuronTLG
Posts: 2
Joined: Mon Jan 06, 2020 3:39 pm

Re: Problem with reading PosData in Request from Website

Post by AuronTLG »

I don't know exactly what I did wrong before, but I replaced the TChromiumWindow with a TChromium + TCEFWindowParent combination, at which point I got it to work easily by using OnBeforeResourceLoad.

The only question that remains for me concerns basically the other way around.

At the beginning of my application I have to submit login data and additional information to the website, which, with the old TWebBrowser was done by simply building the URL like "www.testurl.test?key=value&key2=value2..." and navigating to it, which still works.
I tried, however, to replace that by using OnBeforeResourceLoad to put the parameters into the posdata of the initial request, which is an approach that can be found in the official CEF-Dokumentation:

Code: Select all

// Set the request URL.
request->SetURL(some_url);

// Set the request method. Supported methods include GET, POST, HEAD, DELETE and PUT.
request->SetMethod(“POST”);

// Optionally specify custom headers.
CefRequest::HeaderMap headerMap;
headerMap.insert(
    std::make_pair("X-My-Header", "My Header Value"));
request->SetHeaderMap(headerMap);

// Optionally specify upload content.
// The default “Content-Type” header value is "application/x-www-form-urlencoded".
// Set “Content-Type” via the HeaderMap if a different value is desired.
const std::string& upload_data = “arg1=val1&arg2=val2”;
CefRefPtr<CefPostData> postData = CefPostData::Create();
CefRefPtr<CefPostDataElement> element = CefPostDataElement::Create();
element->SetToBytes(upload_data.size(), upload_data.c_str());
postData->AddElement(element);
request->SetPostData(postData);
I intercepted the initial request, created the postdata with the url-encoded parameter-string analogous to the above code, just in Delphi of course, and assigned it to the request. However, the website so far wasn't able to access the parameters and subsequently complains about them being missing.

From what I read in the documentation and in the forums, what im trying to attempt should be possible without any problems, but so far I don't get it to work.
User avatar
salvadordf
Posts: 4052
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Problem with reading PosData in Request from Website

Post by salvadordf »

Yes, it's possible to do that.

Here you have how to add or replace a custom header in that event :
https://github.com/salvadordf/CEF4Delph ... r.pas#L428

Here you have another example in the TChromium code that appends a custom header :
https://github.com/salvadordf/CEF4Delph ... .pas#L4436

And here you can see how to modify the request to add POST data :
https://github.com/salvadordf/CEF4Delph ... t.pas#L272

The last example uses a new ICefRequest instance but you can do the same with the existing "request" parameter in TChromium.OnBeforeResourceLoad.

If you don't have access to the server try comparing the HTTP traffic with TWebBrowser and TChromium to see what's different.
Post Reply