C# – Capture network traffic in C#


I'm using Selenium, and I'm wishing to capture all cookie packets sent during a page request. I've come across Fiddler core, and a few other derivatives, but can't seem to locate any working examples. Have been working on this with no progress for almost 10 hours so any suggestions will be much appreciated.

Best Solution

BrowserMob Proxy is (quote):

A free utility to help web developers watch and manipulate network traffic from their web applications

I've co-written a .NET library to make it simple to work with that proxy. The code is up on GitHub here and I've blogged a bit more about it, with example code, here. So with those 2 components, and Selenium, hopefully you can get the information you need.

Information is provided in the HAR format, for which you can see details of here. Basically, for a request/response you can retrieve all the information of what is going across the network, cookies, timings etc.

Hopefully this is the kind of thing you are after.

e.g. basic example to iterate through each request cookie for each HAR log entry:

foreach(var entry in harData.Log.Entries)
    foreach (var cookie in entry.Request.Cookies)
        string name = cookie.Name;
        string value = cookie.Value;

        // Do something exciting....or just write the values to Console :)
        Console.Write("Cookie name={0}, value={1}, name, value);
Related Question