Capture all requests with FiddlerCore

user969068 picture user969068 · Feb 26, 2014 · Viewed 7.6k times · Source

I am trying to monitor traffic using FiddlerCore and WebBrowser Controller, I have below code to capture web requests in C#

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

It is only returning just one request response (given url in webBroser.Navigate), I can not see requests for images, css and other loaded files on example site. I could not find any info on this, Can someone please help me to understand on how I can capture all GET POST requests when webBroswer.Navigate to given URL?

updated:

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

Answer

EricLaw picture EricLaw · Feb 26, 2014
  1. What makes you think that the other items aren't being pulled from the cache?
  2. You shouldn't use FiddlerCoreStartupFlags.Default if you're going to use SetProxyInProcess; the former sets the proxy for every process on the system while the latter sets the proxy only for the current process.