使用FiddlerCore捕获所有请求

时间:2014-02-26 20:16:50

标签: c# fiddler fiddlercore

我正在尝试使用FiddlerCore和WebBrowser控制器来监控流量,我有以下代码来捕获C#中的Web请求

    private void button1_Click(object sender, EventArgs e)
    {
       List<Fiddler.Session> oAllSessions = new List<Fiddler.Session>();
       URLMonInterop.SetProxyInProcess("127.0.0.1:8888", "<-loopback>");
        webBrowser1.ScriptErrorsSuppressed = true;
        WebProxy myProxy = new WebProxy();
        Uri newUri = new Uri("http://localhost:8888");
        myProxy.Address = newUri;


        Fiddler.FiddlerApplication.Startup(8888, FiddlerCoreStartupFlags.Default);
        Fiddler.FiddlerApplication.AfterSessionComplete += delegate(Fiddler.Session oS)
        {
                Monitor.Enter(oAllSessions);
                oAllSessions.Add(oS);
                Monitor.Exit(oAllSessions);

        };

        webBrowser1.Navigate("http://www.test.com/");
        while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
        {
            System.Windows.Forms.Application.DoEvents();
        }

        var message = string.Join(Environment.NewLine, oAllSessions);
        textBox1.Text = textBox1.Text  + message;
        Fiddler.FiddlerApplication.Shutdown();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        Fiddler.FiddlerApplication.Shutdown();
        URLMonInterop.ResetProxyInProcessToDefault();
    }

它只返回一个请求响应(在webBroser.Navigate中给定url),我无法在示例站点上看到图像,css和其他已加载文件的请求。我找不到任何关于此的信息,有人可以帮助我了解如何在webBroswer.Navigate到给定URL时捕获所有GET POST请求吗?

更新

  delegate void updateUI();
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Fiddler.FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
        Fiddler.FiddlerApplication.Startup(8888, FiddlerCoreStartupFlags.Default);

        webBrowser1.ScriptErrorsSuppressed = true;
        WebProxy myProxy = new WebProxy();
        Uri newUri = new Uri("http://localhost:8888");
        myProxy.Address = newUri;

        string[] urls = new string[] { "http://localhost/test/page1", 
                                       "http://localhost/test/page2 " 
                                };

        foreach (string url in urls)
        {
            webBrowser1.Navigate(url);
            // Capture root url
            listBox1.Invoke(new updateUI(() =>
            {
                listBox1.Items.Add(url);
            }));
            while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
            {
                System.Windows.Forms.Application.DoEvents();
            }
            // Hack as I am not sure what to do here so wait 10 second for webBrowser to load all requests otherwise I only get last url data in listbox
            for (int i = 0; i <= 10; i++)
            {
                System.Windows.Forms.Application.DoEvents();
                Thread.Sleep(1000);
            }

        }
    }

    void FiddlerApplication_AfterSessionComplete(Session oSession)
    {
        var regex = new Regex("keywords-in-url-to-match");

        // If my desired keyword match then grab request POST body
        if (regex.IsMatch(oSession.fullUrl.ToString()))
        {
            string requestBody = oSession.GetRequestBodyAsString();

            // Capture url and request body. This url is not root url
            listBox1.Invoke(new updateUI(() =>
            {
                 listBox1.Items.Add(oSession.fullUrl); 
                listBox1.Items.Add(System.Web.HttpUtility.UrlDecode(requestBody)); 
            }));
        }
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        Fiddler.FiddlerApplication.Shutdown();
    }

1 个答案:

答案 0 :(得分:2)

  1. 是什么让你认为其他项目没有被从缓存中拉出来?
  2. 如果您要使用FiddlerCoreStartupFlags.Default,则不应使用SetProxyInProcess;前者为系统上的每个进程设置代理,而后者仅为当前进程设置代理。