Modify requestHeaders in “custom” browser in delphi

左心房为你撑大大i 提交于 2020-01-03 17:35:56

问题


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.


回答1:


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;



回答2:


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



来源:https://stackoverflow.com/questions/9210507/modify-requestheaders-in-custom-browser-in-delphi

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