How do I send post and header data with Chromium?

折月煮酒 提交于 2019-12-08 17:00:30

问题


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?


回答1:


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;


来源:https://stackoverflow.com/questions/12978338/how-do-i-send-post-and-header-data-with-chromium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!