简体   繁体   English

如何在C#中使用Fiddlercore捕获HTTP请求?

[英]how to capture http request using Fiddlercore in C#?

I am trying to capture request header using fiddlercore in C#. 我正在尝试使用C#中的fiddlercore捕获请求标头。 Here is my code. 这是我的代码。 I use selenium to get to the webpage for which I want to get Request header/ webforms. 我使用硒到达要获取其请求标头/ Web表单的网页。 I am able to reach webpage but can not capture anything using fiddlercore. 我能够访问网页,但无法使用fiddlercore捕获任何内容。 I know that I have to use delegate and BeginInvoke method but how exactly it should be done is unclear. 我知道我必须使用委托和BeginInvoke方法,但是尚不清楚应该如何完成。 I am using AfterSessionComplete event to capture request body. 我正在使用AfterSessionComplete事件来捕获请求正文。 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: 您应该将Selenium设置为通过FiddlerCore代理,这是您的操作方式:

     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: 忠告,您可以在启动FiddlerCore时设置更多标志,以节省以后的故障排除:

     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: 由于您可能正在使用FiddlerCore + Selenium进行测试,因此您需要添加一些其他内容:

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.Startup(8877,fiddlerStartUpFlags);之前,请执行以下命令-

    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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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