2014-08-28 96 views
1

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

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

吉姆·埃文斯,涉及与硒的开发者之一,广泛覆盖在这里它:http://jimevansmusic.blogspot.co.uk/ – Arran 2014-08-28 10:06:40

回答

2
  1. 您应该设置硒要经过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 +硒进行测试,你将需要添加一些其他的东西:

当测试完成,执行该 -

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