在C#应用程序中使用FiddlerCore捕获应用程序本身引发的单个HTTPS请求和响应
我正在编写一个C#.NET 4.0应用程序,在该应用程序中,我将WebBrowser控件导航到一个安全的网站,以编程方式输入凭据,然后导航到特定页面。我想捕获单击按钮产生的HTTPS请求和响应,如下所示:
I am writing a C# .NET 4.0 application in which I navigate a WebBrowser control to a secure website, programmatically enter the credentials, and navigate to a particular page. I want to capture the HTTPS requests and responses which result from the single click of a button, something like this:
NavigateControlToWebPage("https://fancyDancySite.com/fancyPage");
IHTMLElement btn = doc.getElementById("fancyDancyButton");
StartCapturingMyOwnHTTPSStuffWithFiddlerCore();
btn.click();
StopTheCapturing();
我一直在根据网上的一些例子以及使用SampleApp搞乱这个问题对于FiddlerCore。我根本无法破译如何做StartCapturing方法需要做的事情。我的代码目前是这样,但它没有成功:
I have been messing with this based on a number of examples on the web, and with using the SampleApp for FiddlerCore. I am simply not able to decipher how to do what the StartCapturing method needs to do. My code currently is this, but it does not succeed:
WebProxy myProxy = new WebProxy();
Uri newUri = new Uri("https://localhost:8080");
myProxy.Address = newUri;
if (!Fiddler.CertMaker.rootCertExists())
{
if (!Fiddler.CertMaker.createRootCert())
{
throw new Exception("Unable to create cert for FiddlerCore.");
}
}
if (!Fiddler.CertMaker.rootCertIsTrusted())
{
if (!Fiddler.CertMaker.trustRootCert())
{
throw new Exception("Unable to install FiddlerCore's cert.");
}
}
Fiddler.FiddlerApplication.Startup(8080, Fiddler.FiddlerCoreStartupFlags.DecryptSSL);
Fiddler.FiddlerApplication.AfterSessionComplete += Fiddler_AfterSessionComplete;
ETA:我支持公司代理,试图访问外部https网站。
ETA: I am behind a corporate proxy, trying to access an external https site.
ETA:我有一些效果,因为webbrowser控件获取导航已取消页面而不是通常的网站。但这不是我想要的效果:)
ETA: I am having some effect, because the webbrowser control gets a "Navigation was cancelled" page instead of the usual website. But that is not the effect I want :)
ETA:删除了我认为在这种情况下不合适的SetProxyInProcess
ETA: removed the SetProxyInProcess which I think is not appropriate in this case
ETA:我当前的StartCapturing方法如下:
ETA: My current StartCapturing method is as follows:
public void StartCapturingMyOwnHTTPSStuffWithFiddlerCore()
{
InstallCertificate();
FiddlerApplication.Startup(0, FiddlerCoreStartupFlags.Default);
FiddlerApplication.AfterSessionComplete += Fiddler_AfterSessionComplete;
}
这是有效的,但当我添加& ~RegisterAsSystemProxy 它停止工作。
This works, but when I add & ~RegisterAsSystemProxy it stops working.
WebProxy的所有代码不需要myProxy ,因为这是用于为System.NET的HTTPWebRequests设置Web代理的代码,但Web浏览器控件不使用System.NET HTTP Stack,它使用WinINET的堆栈。
All of your code for WebProxy myProxy is not needed because this is the code used to set the Web Proxy for System.NET's HTTPWebRequests, but the Web Browser control does not use the System.NET HTTP Stack, it uses WinINET's stack.
你设置的 StartupFlags 是不够的;如果要捕获来自所有应用程序的流量,或者使用 FiddlerCoreStartupFlags.Default& ~FiddlerCoreStartupFlags.RegisterAsSystemProxy 如果你打算使用SetProxyInProcess API来获取当前应用程序的流量。
The StartupFlags you're setting are not sufficient; you should use FiddlerCoreStartupFlags.Default instead if you want to capture traffic from all applications, or use FiddlerCoreStartupFlags.Default & ~FiddlerCoreStartupFlags.RegisterAsSystemProxy if you plan to use the SetProxyInProcess API to get traffic only from your current application.
你需要非常小心你的StopTheCapturing代码是如何实现的,因为你很可能太早调用它 - 单击Web浏览器控件中的按钮的调用几乎会立即返回,但Web浏览器控件可能会执行之后的一段时间的操作。
You need to be very very careful in how your "StopTheCapturing" code is implemented, because it's very likely that you're calling it far too early-- the call to click the button in the Web Browser Control will return almost instantly, but the Web Browser control will likely be performing operations for some period of time afterward.