修改“custom”中的requestHeaders delphi 浏览器

发布于 2025-01-04 12:38:26 字数 199 浏览 1 评论 0原文

我的 deplhi 应用程序 (IE) 中集成了一个浏览器。我需要调用某个 Web 应用程序,并且需要在标头中为来自应用程序浏览器的所有请求附加一个新变量,例如 jquery 将 HTTP_X_REQUESTED_WITH 参数添加到 xhrobj 中。我知道如何才能做到这一点吗?代码示例会很棒。我正在使用TWebBrowser

I have a browser integrated in my deplhi application (IE). I need to call a certain web app and I need to append a new variable in the header for all the requests that come from my application's browser, like jquery adds to the xhrobj the HTTP_X_REQUESTED_WITH parameter. Any idea on how can I do that? code samples would be great. I am usin TWebBrowser.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凡间太子 2025-01-11 12:38:26

您可以使用 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;

You can modify the headers with the OnBeforeNavigate2 event:

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;
你怎么敢 2025-01-11 12:38:26

IWebBrowser2.Navigate 有一个参数,可让您定义其他标头。

IWebBrowser2.Navigate has a parameter that lets you define additional headers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文