HttpWatch Automation Reference - Version 13.x
Samples / Page Check Sample for Edge (C#)
In This Topic
    Page Check Sample for Edge (C#)
    In This Topic


    This C# sample program uses the HttpWatch automation interface to record and analyze the HTTP traffic generated by the loading of a single web page in Microsoft Edge.

    Loading the Sample into Visual Studio

    The application is in the following folder:


    It is worth making a working copy of this folder elsewhere; particularly on Windows Vista or later as UAC (User Account Control) will normally stop you making changes to that directory.

    Once you have done this, you can open the application in Visual Studio by double-clicking on the page_check.sln file in the page_check\csharp\ folder.

    Solution Implementation

    The Visual Studio solution contains a single console application project named page_check. If the References node is expanded, you can see that a reference to the HttpWatch automation library has been added. When developing an application from scratch, you will need to create this reference, as described in Using HttpWatch Automation with C#.

    All the functional code for this project is in the pagechecker.cs source file. Since this is a console application, it contains the application entry point method Main().

    Program Operation

    The program starts by asking the user to enter the URL for the web page to monitor. If the user simply hits the Enter key, the HttpWatch home page is used.

    Having obtained the URL, the first step is to create an instance of the Controller class and use it to create a new Edge tab with the HttpWatch extension object attached to it. Note the use of the Controller class's Edge property. This returns a reference to a Edge object, that provides methods for creating the HttpWatch extension and the browser instance:


    Creating a New Instance of the HttpWatch Extension in Edge
    Copy Code
    HttpWatch.Controller controller = new HttpWatch.Controller();
    HttpWatch.Plugin plugin = controller.Edge.New();
    It is possible to use ControllerClass instead of Controller as the object type. ControllerClass is effectively an alias for Controller, createded by the process of importing the Type Library into the .NET environment. However, Controller is the preferred usage.


    We can now activate recording of HTTP requests and responses. Filtering is first disabled by calling the Log object's EnableFilter method. The extension's Record method is then called to start capturing traffic:


    Start Recording HTTP Traffic
    Copy Code


    With the HttpWatch extension primed to record HTTP requests and responses, we instruct the browser to load the page by calling the Plugin object's GotoURL method. This method does not block until the page is fully loaded, so we must wait before examining the recorded data:


    Load a URL
    Copy Code
    control.Wait(plugin, -1);


    The Wait method is called with a timeout value of -1, meaning that an indefinite wait is acceptable. When Wait returns, page loading will have ceased and recording is stopped using the Plugin object's Stop method. 

    The Pages property of the Log object returns is a list of all the Page objects recorded. Individual pages can be referenced with the square-bracket indexing syntax used for arrays in C#. Since the program has only captured the traffic for a single page, we can retrieve it by specifying an index of [0]. The Pages class's Count property is checked to verify that at least one page was recorded:


    Print the Page Title
    Copy Code
    if ( plugin.Log.Pages.Count != 0 )
        Console.WriteLine( "\r\nPage Title: '" + plugin.Log.Pages[0].Title + "'");


    The Page object also has an Entries property that returns a list of Entry objects. Each Entry object represents a single HTTP transaction (request and response) and has over 25 properties relating to the HTTP request and response messages.

    The sample program then displays summary statistics for the first page. This information is obtained by querying the Summary property of the page's Entries object. The Summary object it returns contains various items of statistical data:


    Displaying Statistics for the First Page
    Copy Code
    // Display summary statistics for whole log
    Summary summary = plugin.Log.Pages[0].Entries.Summary;
    Console.WriteLine( "Total time to load page (secs):      " + summary.Time);
    Console.WriteLine( "Number of bytes received on network: " + summary.BytesReceived);
    Console.WriteLine( "HTTP compression saving (bytes):     " + summary.CompressionSavedBytes);Console.WriteLine( "Number of round trips:               " + summary.RoundTrips);
    Console.WriteLine( "Number of errors:                    " + summary.Errors.Count);

    Finally, the application closes the browser, using the extension's CloseBrowser method.

    Running the Application

    To run the application from Visual Studio, drop down the Debug menu and click Start Without Debugging (or hold the Ctrl key down and press the F5 key). The program will prompt for a URL and then load the page in Edge. Once the page is loaded there will be a short pause before Edge is closed and the results are displayed in the console window.

    See Also