首页 » 文章随笔 » 技巧教程 » 如何自定义修改TChromium的header头信息!

如何自定义修改TChromium的header头信息!

TChromium自定义post的header信息提交数据

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;

在另一版本中的代码

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;

TChromium自定义post数据

function CreateField(const AValue: AnsiString): ICefPostDataElement;
begin
 Result := TCefPostDataElementRef.New;
 Result.SetToBytes(Length(AValue), PAnsiChar(AValue));
end;

//*****************************************

procedure POST(total, fee: integer);
var
 Header: ICefStringMultimap;
 Data: ICefPostData;
 Request: ICefRequest;
 Cook: ICefCookieManager;
begin
 Request := TCefRequestRef.New;
 Request.Url := '';
 Request.Method := 'POST';
 Request.Flags:=[UR_FLAG_ALLOW_CACHED_CREDENTIALS,UR_FLAG_ALLOW_COOKIES];
 
 Header := TCefStringMultimapOwn.Create;
 Header.Append('Host', '');
 Header.Append('User-Agent', '');
 Header.Append('Accept', '');
 Header.Append('Accept-Language', '');
 Header.Append('Accept-Encoding', '');
 Header.Append('Content-Type', '');
 Header.Append('Referer', '');
 Header.Append('Content-Length', '');
 Header.Append('Origin', '');
 Header.Append('Connection', '');
 Header.Append('Pragma', '');
 Header.Append('Cache-Control', '');

 Header.Append('Cookie','...');
 ...
 Header.Append('Cookie','...');
 
 Request.SetHeaderMap(Header);

 Data := TCefPostDataRef.New;
 Data.AddElement(CreateField('sessionid='+SessionID));
 Data.AddElement(CreateField('¤cy=5'));
 Data.AddElement(CreateField('&subtotal='+inttostr(total-fee)));
 Data.AddElement(CreateField('&fee='+IntToStr(fee)));
 Data.AddElement(CreateField('&total='+IntToStr(total)));

 Request.PostData := Data;

 form1.Chromium1.Browser.MainFrame.LoadRequest(Request);
end;

 

未经允许不得转载:百脑问 » 如何自定义修改TChromium的header头信息!

×

感谢您的支持,我们会一直保持!

扫码支持
请土豪扫码随意打赏

打开微信扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

分享到:更多 ()

评论 抢沙发

评论前必须登录!

百脑问 创新坚持 杰然不同

赞助我们联系我们
数据查询次数:20 消耗时间:0.547 秒, 使用内存: 21.01MB
您好,有什么可以帮您吗!