如何检索XHR响应?

时间:2015-05-22 22:33:03

标签: delphi delphi-2007 chromium-embedded

基本上,我使用Chromium来显示一个我无法控制的网站。当用户点击特定按钮时,发送XHR请求;我希望能够找到答案。所以,假设请求发送A,服务器回复B;我希望能够阅读B.我怎么能这样做?

1 个答案:

答案 0 :(得分:6)

缺少OnResourceResponse事件

在一个好老的CEF1中你可以简单地使用OnResourceResponse事件。在CEF3中,这样一个看似微不足道的任务可能会成为一个真正的挑战,因为Issue 515the answer与你在这里提出的根本相同的问题仍然是打开的,这似乎是唯一的方法(此时)正在实现您自己的CefResourceHandler处理程序,以便在浏览器和外部的世界之间建立代理。实现原则在similar topic中描述,如:

  

您可以使用CefResourceHandler via   CefRequestHandler :: GetResourceHandler并执行请求/返回   使用CefURLRequest自己回复内容。

所以这里是在DCEF3(目前)做什么:

1。定义自己的资源处理程序

首先派生自己的TCefResourceHandlerOwn后代,其中至少实现以下方法:

  • ProcessRequest - 在这里,您将转发(发送)请求A到服务器并接收服务器的响应B(这是您的第一次机会)使用响应数据B),你应该保存它(理想情况下在类字段中,以便可以轻松地刷新到ReadResponse方法的输出缓冲区。)

  • GetResponseHeaders - 在这种方法中,您需要填写有关响应B数据长度和(某些)标题字段的输出参数(这可能是您需要的地方)让您的响应B解析为标题以填充CefResponse类型参数成员。)

  • ReadResponse - 您可以在此处刷新您的回复B数据,以便浏览器进行最终处理。

2。将资源处理程序分配给请求

下一步是为请求分配您自己的资源处理程序。这在技术上就像从Chromium的OnGetResourceHandler事件处理程序返回对资源处理程序接口的引用一样简单。但是在这一步中你应该考虑越是缩小标准,你就越容易在你的资源处理程序中生活。因为如果您分配处理程序,例如对于任何请求(对于任何URL),您可能必须处理并过滤掉与您的整体任务完全无关的服务器中的响应。

因此,我建议将分配范围缩小到该Web应用程序按钮生成的请求,或者至少是请求资源类型(在这种情况下为RT_XHR),这样您就赢了需要自己处理所有请求。

代码示例

如何实施上述步骤有很多方法。在这个例子中,我插入了Chromium浏览器类,并添加了一个新事件OnXmlHttpExchange,当XHR请求完成时(但在它传递给浏览器之前,它会允许您甚至修改响应)。

uses
  CefLib, CefVCL;

type
  TXmlHttpExchangeEvent = procedure(Sender: TObject; const Request: ICefRequest; const Response: ICefResponse; DataStream: TMemoryStream) of object;

  TChromium = class(CefVCL.TChromium)
  private
    FOnXmlHttpExchange: TXmlHttpExchangeEvent;
  protected
    procedure DoXmlHttpExchange(const Request: ICefRequest; const Response: ICefResponse; DataStream: TMemoryStream); virtual;
    function doOnGetResourceHandler(const Browser: ICefBrowser; const Frame: ICefFrame; const Request: ICefRequest): ICefResourceHandler; override;
  public
    property OnXmlHttpExchange: TXmlHttpExchangeEvent read FOnXmlHttpExchange write FOnXmlHttpExchange;
  end;

  TXmlHttpHandler = class(TCefResourceHandlerOwn)
  private
    FOwner: TChromium;
    FOffset: NativeUInt;
    FStream: TMemoryStream;
    FCallback: ICefCallback;
    FResponse: ICefResponse;
  protected
    function ProcessRequest(const Request: ICefRequest; const Callback: ICefCallback): Boolean; override;
    procedure GetResponseHeaders(const Response: ICefResponse; out ResponseLength: Int64; out RedirectUrl: ustring); override;
    function ReadResponse(const DataOut: Pointer; BytesToRead: Integer; var BytesRead: Integer; const Callback: ICefCallback): Boolean; override;
  public
    constructor Create(Owner: TChromium; const Browser: ICefBrowser; const Frame: ICefFrame; const SchemeName: ustring; const Request: ICefRequest); reintroduce;
    destructor Destroy; override;
    procedure WriteResponse(const Request: ICefUrlRequest; Data: Pointer; Size: NativeUInt); virtual;
    procedure CompleteRequest(const Request: ICefUrlRequest); virtual;
  end;

  TXmlHttpRequestClient = class(TCefUrlrequestClientOwn)
  private
    FHandler: TXmlHttpHandler;
  protected
    procedure OnDownloadData(const Request: ICefUrlRequest; Data: Pointer; DataLength: NativeUInt); override;
    procedure OnRequestComplete(const Request: ICefUrlRequest); override;
  public
    constructor Create(Handler: TXmlHttpHandler); reintroduce;
  end;

implementation

{ TChromium }

procedure TChromium.DoXmlHttpExchange(const Request: ICefRequest; const Response: ICefResponse; DataStream: TMemoryStream);
begin
  // fire the OnXmlHttpExchange event
  if Assigned(FOnXmlHttpExchange) then
    FOnXmlHttpExchange(Self, Request, Response, DataStream);
end;

function TChromium.doOnGetResourceHandler(const Browser: ICefBrowser; const Frame: ICefFrame; const Request: ICefRequest): ICefResourceHandler;
begin
  // first trigger the browser's OnGetResourceHandler event
  Result := inherited;
  // if no handler was assigned and request is of type XHR, create our custom one
  if not Assigned(Result) and (Request.ResourceType = RT_XHR) then
    Result := TXmlHttpHandler.Create(Self, Browser, Frame, 'XhrIntercept', Request);
end;

{ TXmlHttpHandler }

constructor TXmlHttpHandler.Create(Owner: TChromium; const Browser: ICefBrowser; const Frame: ICefFrame; const SchemeName: ustring; const Request: ICefRequest);
begin
  inherited Create(Browser, Frame, SchemeName, Request);
  FOwner := Owner;
  FStream := TMemoryStream.Create;
end;

destructor TXmlHttpHandler.Destroy;
begin
  FStream.Free;
  inherited;
end;

function TXmlHttpHandler.ProcessRequest(const Request: ICefRequest; const Callback: ICefCallback): Boolean;
begin
  Result := True;
  // reset the offset value
  FOffset := 0;
  // store the callback reference
  FCallback := Callback;
  // create the URL request that will perform actual data exchange (you can replace
  // it with any other; e.g. with MSXML, or an Indy client)
  TCefUrlRequestRef.New(Request, TXmlHttpRequestClient.Create(Self));
end;

procedure TXmlHttpHandler.GetResponseHeaders(const Response: ICefResponse; out ResponseLength: Int64; out RedirectUrl: ustring);
var
  HeaderMap: ICefStringMultimap;
begin
  // return the size of the data we have in the response stream
  ResponseLength := FStream.Size;
  // fill the header fields from the response returned by the URL request
  Response.Status := FResponse.Status;
  Response.StatusText := FResponse.StatusText;
  Response.MimeType := FResponse.MimeType;
  // copy the header map from the response returned by the URL request
  HeaderMap := TCefStringMultimapOwn.Create;
  FResponse.GetHeaderMap(HeaderMap);
  if HeaderMap.Size <> 0 then
    FResponse.SetHeaderMap(HeaderMap);
end;

function TXmlHttpHandler.ReadResponse(const DataOut: Pointer; BytesToRead: Integer; var BytesRead: Integer; const Callback: ICefCallback): Boolean;
begin
  // since this method can be called multiple times (reading in chunks), check if we
  // have still something to transfer
  if FOffset < FStream.Size then
  begin
    Result := True;
    BytesRead := BytesToRead;
    // copy the data from the response stream to the browser buffer
    Move(Pointer(NativeUInt(FStream.Memory) + FOffset)^, DataOut^, BytesRead);
    // increment the offset by the amount of data we just copied
    Inc(FOffset, BytesRead);
  end
  else
    Result := False;
end;

procedure TXmlHttpHandler.WriteResponse(const Request: ICefUrlRequest; Data: Pointer; Size: NativeUInt);
begin
  // write the just downloaded data to the intermediate response stream
  FStream.Write(Data^, Size);
end;

procedure TXmlHttpHandler.CompleteRequest(const Request: ICefUrlRequest);
begin
  FStream.Position := 0;
  // store the response reference for the GetResponseHeaders method
  FResponse := Request.GetResponse;
  // this method is executed when the URL request completes, so we have everything we
  // need to trigger the OnXmlHttpExchange event
  FOwner.DoXmlHttpExchange(Request.GetRequest, FResponse, FStream);
  // this signals the handler that the request has completed and that it can process
  // the response headers and pass the content to the browser
  if Assigned(FCallback) then
    FCallback.Cont;
end;

{ TXmlHttpRequestClient }

constructor TXmlHttpRequestClient.Create(Handler: TXmlHttpHandler);
begin
  inherited Create;
  FHandler := Handler;
end;

procedure TXmlHttpRequestClient.OnDownloadData(const Request: ICefUrlRequest; Data: Pointer; DataLength: NativeUInt);
begin
  FHandler.WriteResponse(Request, Data, DataLength);
end;

procedure TXmlHttpRequestClient.OnRequestComplete(const Request: ICefUrlRequest);
begin
  FHandler.CompleteRequest(Request);
end;

可能使用这个插入的类:

type
  TForm1 = class(TForm)
    Chromium: TChromium;
    procedure FormCreate(Sender: TObject);
  private
    procedure XmlHttpExchange(Sender: TObject; const Request: ICefRequest; const Response: ICefResponse; DataStream: TMemoryStream);
  end;

implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chromium.OnXmlHttpExchange := XmlHttpExchange;
end;

procedure TForm1.XmlHttpExchange(Sender: TObject; const Request: ICefRequest; const Response: ICefResponse; DataStream: TMemoryStream);
begin
  // here you can find the request for which you want to read the response (explore the
  // Request parameter members to see by what you can do this)
  if Request.Url = 'http://example.com' then
  begin
    // the DataStream stream contains the response, so process it here as you wish; you
    // can even modify it here if you want
    DataStream.SaveToFile(...);
  end;
end;