Capturing Awesomium Requests and Responses

The Awesomium browser controls provides a set of events that the hosting application can handle, but there is (currently, as of 1.7.4.2) there is no way to access the underlying HTTP requests and responses.

There are a couple of questions on SO that try to solve this problem, such as

which all hint to use the FiddlerCore library to make the application hosting the browser control also act as proxy for this control.

By using the proxy functionality of Fiddler and handling its events it is possible to access the HTTP request and response, and, for example, log data retrieved in AJAX calls. This answer on SO shows how Awesomium’s proxy settings are defined.

Since not all code samples on SO work with the current Awesomium version 1.7.4.2, here my solution based on the articles above.

In the App class (App.xml.cs), FiddlerCode must be initialized.

  public partial class App : Application
  {
    log4net.ILog logger;

    protected override void OnStartup(StartupEventArgs e)
    {
      logger = log4net.LogManager.GetLogger(typeof(App));
      SetupInternalProxy();
      base.OnStartup(e);
    }

    private void SetupInternalProxy()
    {
      FiddlerApplication.AfterSessionComplete += 
        FiddlerApplication_AfterSessionComplete;
      FiddlerApplication.Log.OnLogString += 
        (o, args) => logger.Warn(args.LogString);

      FiddlerCoreStartupFlags oFCSF = FiddlerCoreStartupFlags.Default;
      //this line is important as it will avoid changing the proxy for the whole system.
      oFCSF = (oFCSF & ~FiddlerCoreStartupFlags.RegisterAsSystemProxy);

      FiddlerApplication.Startup(
        0,
        oFCSF
        );
    }

This is also the location to analyze requests and responses. In the sample, jQuery JSON-responses are deserialized into C# objects

    private void FiddlerApplication_AfterSessionComplete(Session oSession)
    {
      var resp = oSession.oResponse;
      var ct = resp.headers["Content-Type"];

      if (ct.Contains(';'))
        ct = ct.Split(";".ToCharArray())[0];

      var req = oSession.oRequest;
      
      switch (ct)
      {
        case "text/html":
          // do something
          break;

        case "application/json":
          var json = oSession.GetResponseBodyAsString();
          if (json.StartsWith("jQuery"))
          {
            json = json.Substring(json.IndexOf('(') + 1);
            json = json.Substring(0, json.Length - 2);

            // deserialize C# object
            var content = JsonConvert.DeserializeObject(json);

            if (content != null)
            {
              var uri = new Uri(oSession.fullUrl);
              var q = HttpUtility.ParseQueryString(uri.Query);
              // parse query string
            }
          }
          break;

        default:
          // 
          break;
      }
    }

In the window hosting the browser control, we need to set the browser control’s proxy settings in the constructor:

public MyWindow()
{
  InitializeComponent();

  var pref = new awe.WebPreferences
  {
    ProxyConfig = "http://127.0.0.1:" + 
      FiddlerApplication.oProxy.ListenPort.ToString(),
    Plugins = false,
  };
  this.webControl.WebSession = awe.WebCore.CreateWebSession(pref);
}

That’s it 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: