I am attempting to convert some code from TWebBrowser to Chromium but am having trouble figuring out how to send post and header data with an HTTP request.
Below is the TWebBrowser functionality I'm trying to implement.
var
VHeader, PostData: OleVariant;
PostData := VarArrayCreate([0, Length(XMLString) - 1], varByte) ;
HeaderData := 'Content-Type: application/x-www-form-urlencoded'+ '\n';
WebBrowser1.Navigate(StrUrl,EmptyParam,EmptyParam,PostData,VHeader);
How do I do the equivalent with Chromium?
Due to a missing documentation for Delphi Chromium Embedded, I'll refer the needed requirements for sending web requests for the C++ version of CEF. So, you need to use the LoadRequest
method for sending requests in Chromium. For using it, you need the object instance of the CefRequest
request object class along with the HeaderMap
and CefPostData
objects for request header and data specification.
Expanding on Henri Gourvest's (author of the Delphi CEF wrapper) example from this thread
, you can in Delphi try something like the following pseudo-code:
uses
ceflib;
function CreateField(const AValue: AnsiString): ICefPostDataElement;
begin
Result := TCefPostDataElementRef.New;
Result.SetToBytes(Length(AValue), PAnsiChar(AValue));
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Header: ICefStringMultimap;
Data: ICefPostData;
Request: ICefRequest;
begin
Header := TCefStringMultimapOwn.Create;
Header.Append('Content-Type', 'application/x-www-form-urlencoded');
Data := TCefPostDataRef.New;
Data.AddElement(CreateField('Data.id=27'));
Data.AddElement(CreateField('&Data.title=title'));
Data.AddElement(CreateField('&Data.body=body'));
Request := TCefRequestRef.New;
Request.Flags := WUR_FLAG_NONE;
Request.Assign('http://example.com/', 'POST', Data, Header);
Chromium1.Browser.MainFrame.LoadRequest(Request);
end;
The same should do another version of the above code:
procedure TForm1.Button1Click(Sender: TObject);
var
Header: ICefStringMultimap;
Data: ICefPostData;
Request: ICefRequest;
begin
Request := TCefRequestRef.New;
Request.Url := 'http://example.com/';
Request.Method := 'POST';
Request.Flags := WUR_FLAG_NONE;
Header := TCefStringMultimapOwn.Create;
Header.Append('Content-Type', 'application/x-www-form-urlencoded');
Request.SetHeaderMap(Header);
Data := TCefPostDataRef.New;
Data.AddElement(CreateField('Data.id=27'));
Data.AddElement(CreateField('&Data.title=title'));
Data.AddElement(CreateField('&Data.body=body'));
Request.PostData := Data;
Chromium1.Browser.MainFrame.LoadRequest(Request);
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With