how to capture http request using Fiddlercore in C

2019-02-28 10:03发布

I am trying to capture request header using fiddlercore in C#. Here is my code. I use selenium to get to the webpage for which I want to get Request header/ webforms. I am able to reach webpage but can not capture anything using fiddlercore. I know that I have to use delegate and BeginInvoke method but how exactly it should be done is unclear. I am using AfterSessionComplete event to capture request body. However it is empty. What am I missing? Can someone please help me solve this issue? Thanks. Here is my code.

    public void requestURL()
{
        IWebDriver driver = new FirefoxDriver();

        driver.Navigate().GoToUrl("http://www.google.com");            

        IWebElement query = driver.FindElement(By.Name("q"));
        // search Cheese
        query.SendKeys("Cheese");
        //// submit query
        query.Submit();

        // Wait for the page to load, timeout after 10 seconds
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
        wait.Until((d) => { return d.Title.StartsWith("Cheese"); });

        // Should see: "Cheese - Google Search"
        Console.WriteLine("Page title is: " + driver.Title);
        Console.WriteLine("URL for page is: " + driver.Url);
    }

    static void Main(string[] args)
    {
        FiddlerApplication.Startup(8877, FiddlerCoreStartupFlags.DecryptSSL);
        HttpActions h = new HttpActions();
        h.requestURL();
        FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
        FiddlerApplication.Shutdown();
    }

    static void FiddlerApplication_AfterSessionComplete(Session oSession)
    {
        var s = oSession.GetRequestBodyAsString();
    }

1条回答
一纸荒年 Trace。
2楼-- · 2019-02-28 10:48
  1. You should set Selenium to go through the FiddlerCore proxy, this is how you do it:

    var seleniumProxy = new Proxy { HttpProxy = "localhost:8878", SslProxy = "localhost:8878" };
    var profile = new FirefoxProfile();
    profile.SetProxyPreferences(seleniumProxy);
    var slenium = new FirefoxDriver(profile);
    
  2. Advice, you can set some more flags when starting the FiddlerCore in order to save you some troubleshooting in the future:

    const FiddlerCoreStartupFlags fiddlerStartUpFlags = FiddlerCoreStartupFlags.DecryptSSL & FiddlerCoreStartupFlags.AllowRemoteClients & FiddlerCoreStartupFlags.CaptureFTP & FiddlerCoreStartupFlags.ChainToUpstreamGateway & FiddlerCoreStartupFlags.MonitorAllConnections & FiddlerCoreStartupFlags.CaptureLocalhostTraffic;
    
    FiddlerApplication.Startup(8877, fiddlerStartUpFlags);
    
  3. Since you are probably using FiddlerCore + Selenium for testing, you will need to add some other stuff:

When a test finish, execute this -

    FiddlerApplication.oProxy.PurgeServerPipePool();//Run this between tests to make sure the new test will start "clean"

Before calling FiddlerApplication.Startup(8877, fiddlerStartUpFlags);, execute these -

    FiddlerApplication.Prefs.SetStringPref("fiddler.config.path.makecert", @"d:\..\Makecert.exe");//To define the MakeCert.exe path manually.
    FiddlerApplication.Prefs.SetBoolPref("fiddler.network.streaming.abortifclientaborts", true);//Abort session when client abort
查看更多
登录 后发表回答