Change requestHeaders in a "normal" browser in delphi

I have a browser integrated into my deplhi (IE) application. I need to call a specific web application, and I need to add a new variable in the header for all requests coming from my application browser, for example, jquery adds the HTTP_X_REQUESTED_WITH parameter to HTTP_X_REQUESTED_WITH . Any idea on how I can do this? code samples would be great. I am using TWebBrowser .

+4
source share
2 answers

You can change the headers using 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; 
+6
source

IWebBrowser2.Navigate has a parameter that allows you to define additional headers.

+1
source

Source: https://habr.com/ru/post/1395593/


All Articles