I want to modify the headers of a request with TWebBrowser in Delphi XE5. As described in Modify requestHeaders in “custom” browser in delphi, you can do that using the OnBeforeNavigate2 event of the TWebBrowser. The procedure corresponding to the event would be:
procedure TForm1.WebBrowser1BeforeNavigate2(Sender: TObject;
const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
Headers: OleVariant; var Cancel: WordBool);
var
NewHeaders: OleVariant;
begin
// some code to stop the navigation, enhance the headers and avoid re-enterng the procedure
// 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;
// now proceed with navigation
(pDisp as IWebBrowser2).Navigate(URL, Flags, TargetFrameName, PostData, NewHeaders);
end;
end;
The problem is that while the method proposed in the answer works for a GET request, for a POST request I run into the following problem: PostData only contains the beginning of the initially posted data (in my case a text file uploaded via a html form). Concretely, while a correct example of what a client sends back to the server would look like this:
--AaB03x
content-disposition: form-data; name="field1"
Joe Blow
--AaB03x
content-disposition: form-data; name="pics"; filename="file1.txt"
Content-Type: text/plain
... contents of file1.txt ...
--AaB03x--
what I find while debugging as PostData is something like this PostString
'-----------------------------7e16'#$D#$A'Content-Disposition: form-data; name="myform"; filename="MyUploadedFile.csv"'#$D#$A'Content-Type: application/vnd.ms-excel'#$D#$A#$D#$A#0
Hence the entire content of the file is missing and the final boundary (i.e. -----------------------------7e16).
WireShark tells me that despite PostData is not NULL Navigate()
is not making a POST request but a GET with a message body being exactly what I found during debugging and show above.
My guess is that the reason for the latter is that the Navigate()
function performs a check on whether PostData is valid before it creates a POST request and it makes a GET otherwise.
Since someone claims that Navigate()
works correctly with PostData what can I do to get the complete PostData for my OnBeforeNavigate2
event?