如何使用Chromium发送帖子和标题数据? [英] How do I send post and header data with Chromium?
问题描述
我正在尝试将一些代码从TWebBrowser转换为Chromium,但是在弄清楚如何使用HTTP请求发送帖子和标题数据时遇到了麻烦。
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.
下面是我要实现的TWebBrowser功能。
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);
我该如何使用Chromium?
How do I do the equivalent with Chromium?
推荐答案
由于缺少有关Delphi Chromium Embedded的文档,我将介绍发送CEF C ++版本的Web请求所需的要求。因此,您需要使用 LoadRequest
方法在Chromium中发送请求。要使用它,您需要 CefRequest
请求对象类以及 HeaderMap
和 CefPostData
对象用于请求标头和数据规范。
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.
从 此线程
,您可以在Delphi中尝试以下伪代码:
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;
这篇关于如何使用Chromium发送帖子和标题数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!