问题
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();
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: I am behind a corporate proxy, trying to access an external https site.
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: removed the SetProxyInProcess which I think is not appropriate in this case
ETA: My current StartCapturing method is as follows:
public void StartCapturingMyOwnHTTPSStuffWithFiddlerCore()
{
InstallCertificate();
FiddlerApplication.Startup(0, FiddlerCoreStartupFlags.Default);
FiddlerApplication.AfterSessionComplete += Fiddler_AfterSessionComplete;
}
This works, but when I add & ~RegisterAsSystemProxy
it stops working.
回答1:
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.
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.
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.
来源:https://stackoverflow.com/questions/39882846/using-fiddlercore-in-a-c-sharp-application-to-capture-a-single-https-request-and