简体   繁体   中英

how to capture http request using Fiddlercore in C#?

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM