【问题标题】:how to capture http request using Fiddlercore in C#?如何在 C# 中使用 Fiddlercore 捕获 http 请求?
【发布时间】:2014-08-28 05:43:02
【问题描述】:

我正在尝试在 C# 中使用 fiddlercore 捕获请求标头。这是我的代码。我使用 selenium 访问我想要获取请求标头/网络表单的网页。我可以访问网页,但无法使用 fiddlercore 捕获任何内容。我知道我必须使用委托和 BeginInvoke 方法,但具体应该如何完成尚不清楚。 我正在使用 AfterSessionComplete 事件来捕获请求正文。然而它是空的。我错过了什么?有人可以帮我解决这个问题吗?谢谢。这是我的代码。

    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();
    }

【问题讨论】:

标签: c# selenium fiddlercore


【解决方案1】:
  1. 您应该将 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. 建议,您可以在启动 FiddlerCore 时设置更多的标志,以便为您节省一些将来的故障排除:

    const FiddlerCoreStartupFlags fiddlerStartUpFlags = FiddlerCoreStartupFlags.DecryptSSL & FiddlerCoreStartupFlags.AllowRemoteClients & FiddlerCoreStartupFlags.CaptureFTP & FiddlerCoreStartupFlags.ChainToUpstreamGateway & FiddlerCoreStartupFlags.MonitorAllConnections & FiddlerCoreStartupFlags.CaptureLocalhostTraffic;
    
    FiddlerApplication.Startup(8877, fiddlerStartUpFlags);
    
  3. 由于您可能正在使用 FiddlerCore + Selenium 进行测试,因此您需要添加一些其他内容:

当一个测试完成时,执行这个 -

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

在调用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    相关资源
    最近更新 更多