Recently, I had a requirement to intercept and analyse ALL communication in a webbrowser control. I think the technique I used can help you.
What you need :
- Awesomium.Net: a Chromium engine based control for .net
- Fiddler Core : an http in-memory proxy, that allows you to monitor http communication.
- HtmlAgility pack : depending on the solution you choose, HAP can help you to dynamically change the DOM of html content, in a FAR MORE RELIABLE way than a regex.
I choose to use Awesomium because it provides a lot more features than the out of the box web browser control. In my case, it allows me to defines the proxy to use instead of the system-wide setting.
Fiddler Core is used to intercept communication. Its API provide ways to intercept/tamper/... when request are issued. In my case, I was only forwarding response bodies to my business classes, but in your case, you should be able to filter on mime-type to either change the HTML DOM (Use HtmlAgility pack !!!!!) or return non 200 http status for images.
Here is the code I used. My app is WPF, but you can adapt it to winform with few efforts :
public partial class App : Application
{
static App()
{
// First, we set up the internal proxy
SetupInternalProxy();
// The we set up the awesomium engine
SetupBrowser();
}
private static void SetupInternalProxy()
{
// My requirement is to get response content, so I use this event.
// You may use other handlers if you have to tamper data.
FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
FiddlerApplication.Log.OnLogString += (o, s) => Debug.WriteLine(s);
FiddlerCoreStartupFlags oFCSF = FiddlerCoreStartupFlags.Default;
//this line is important as it will avoid changing the proxy for the whole system.
oFCSF = (oFCSF & ~FiddlerCoreStartupFlags.RegisterAsSystemProxy);
FiddlerApplication.Startup(0, oFCSF);
}
private static void SetupBrowser()
{
// We may be a new window in the same process.
if (!WebCore.IsRunning)
{
// Setup WebCore with plugins enabled.
WebCoreConfig config = new WebCoreConfig
{
// Here we plug the internal proxy to the awesomium engine
ProxyServer = "http://127.0.0.1:" + FiddlerApplication.oProxy.ListenPort.ToString(),
// Adapt others options related to your needs
EnablePlugins = true,
SaveCacheAndCookies = true,
UserDataPath = Environment.ExpandEnvironmentVariables(@"%APPDATA%\MyApp"),
};
WebCore.Initialize(config);
}
else
{
throw new InvalidOperationException("WebCore should be already running");
}
}
// Here is the handler where I intercept the response
private static void FiddlerApplication_AfterSessionComplete(Session oSession)
{
// Send to business objects
DoSomethingWith(
oSession.PathAndQuery,
oSession.ResponseBody,
oSession["Response", "Content-Type"]
);
}
}
As I said in the comment, you may use another event handler that AfterSessionComplete. It will depends on your requirement (read the fiddler core SDK to get help).
A final word: this code run from the app class (equivalent of Program class in Winform). You may require to use a messaging system or publish a global event (beware of memory leak) in order to use the result in a windows class. You also have to aware that the AfterSessionComplete event is fired from multiple threads, sometimes simultaneously. You will use some kind of Invoking to work in the UI thread.