在Delphi中的“自定义”浏览器中修改请求头

6

我在我的Delphi应用程序中集成了一个浏览器(IE),我需要调用某个Web应用程序并在所有来自我的应用程序浏览器的请求头中添加新变量,就像jQuery添加到xhrobj一样HTTP_X_REQUESTED_WITH参数。您有任何想法吗?代码示例很好。我正在使用TWebBrowser。

2个回答

8
您可以使用 OnBeforeNavigate2 事件修改头信息:
procedure TForm1.WebBrowser1BeforeNavigate2(Sender: TObject;
  const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
  Headers: OleVariant; var Cancel: WordBool);
var
  NewHeaders: OleVariant;
begin
  // do not allow frames or iframes to raise this event
  if (pDisp as IUnknown) = (WebBrowser1.ControlInterface as IUnknown) then
  begin
    // avoid stack overflow: check if our custom header is already set
    if Pos('MyHeader', Headers) <> 0 then Exit;

    // cancel the current navigation
    Cancel := True;
    (pDisp as IWebBrowser2).Stop;

    // modify headers with our custom header
    NewHeaders := Headers + 'MyHeader: Value'#13#10;

    (pDisp as IWebBrowser2).Navigate2(URL, Flags, TargetFrameName, PostData, NewHeaders);
  end;
end;

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接