如何在Delphi TWebBrowser中获取Flash发送的POST数据及服务器响应?
Great question! I’ve tackled this exact scenario before when maintaining a legacy Delphi app that relied on TWebBrowser and embedded Flash content. Ditching external tools like Fiddler and capturing the data directly in-app is totally doable—here are three reliable approaches, ordered from most integrated to most flexible:
The TWebBrowser is built on IE’s URLMoniker system, which lets you hook into the HTTP request pipeline using COM interfaces. This is the most "native" way for Delphi apps:
- Implement
IHttpNegotiate2: This interface fires events at key stages of the HTTP request lifecycle. TheBeginningTransactionmethod lets you inspect and log request headers before they’re sent to the server. - Capture POST Body with Stream Wrapping: For POST data, you’ll need to wrap the
IStreamthat URLMoniker uses to send the request body. Create a custom stream class that delegates to the original stream but records all data written to it. - Example Snippet:
You’ll need to associate this custom negotiator with your TWebBrowser instance by querying its underlyingtype TCustomHttpNegotiate = class(TInterfacedObject, IHttpNegotiate2) public // Implement IHttpNegotiate2 methods here function BeginningTransaction(const szURL: PWideChar; const szHeaders: PWideChar; dwReserved: DWORD; out szAdditionalHeaders: PWideChar): HResult; stdcall; // ... other required interface methods end; function TCustomHttpNegotiate.BeginningTransaction(const szURL: PWideChar; const szHeaders: PWideChar; dwReserved: DWORD; out szAdditionalHeaders: PWideChar): HResult; begin // Log the request URL and headers to your app's storage LogRequestDetails(szURL, szHeaders); Result := S_OK; end;IClientSiteorIWebBrowser2interfaces and linking the negotiator to the browser’s request pipeline.
Since Flash (running in the IE context) uses Windows’ WinINet library to send HTTP requests, hooking key WinINet functions lets you capture all HTTP traffic from the process—including Flash-initiated POSTs:
- Target Key Functions: Hook
HttpSendRequest(to capture request headers and trigger body capture) andHttpSendRequestEx(for chunked requests). To get the POST body, you’ll need to hook the stream write functions used by WinINet, likeWriteFile. - Capture Responses: Hook
InternetReadFileto read and log the server’s response data as it comes in. - Tooling Tip: Use a Delphi-friendly hooking library like EasyHook, or roll your own using detours (just be mindful of 32/64-bit compatibility and system permissions).
- Pro Tip: Always unhook the functions when your app shuts down to avoid system instability.
If your app targets IE11 or newer, you can leverage the IE DevTools Protocol to capture network traffic via a WebSocket connection. This is a cleaner, more modern approach:
- Enable DevTools via Registry: First, set a registry key to enable remote debugging for IE instances.
- Launch TWebBrowser with Debug Port: When initializing your TWebBrowser, pass a command-line argument to enable the debug port (e.g.,
--remote-debugging-port=9222). - Connect via WebSocket: Use a Delphi WebSocket component to connect to
ws://localhost:9222/jsonto get the target debug URL, then send theNetwork.enablecommand to start capturing requests. - Listen for Events: Subscribe to
Network.requestWillBeSent(for full request data, including POST body) andNetwork.responseReceived(for server responses).
Important Notes
- Flash is end-of-life, so if possible, consider migrating away from it long-term—but these methods work reliably for legacy setups.
- For 64-bit apps, ensure your hooking/interface code is compiled for 64-bit to avoid memory access errors.
- Always test thoroughly to make sure your intercept code doesn’t break the original request/response flow.
内容的提问来源于stack exchange,提问作者Chris

