Access the wordpress browser check api from

New versions of WordPress have a pretty handy widget included with the admin dashboard.  The widget checks the version of the browser that the user is running (via the user agent) and alerts them if it is insecure (ie6) or has an upgrade (Firefox 3.x).  I wanted to include a similar widget with Acturent and if possible, take advantage of their service api.

After a short time tinkering, I have a nice c# wrapper to their api.  The code below uses a class called BiaCache that is basically a wrapper around System.Web.Cache or an in-memory dictionary, based on if System.Web.Cache exists.  A slight modification will be needed based on your usage.  I cache the results for a week, based on the user agent.

To deserialize the string that comes back from the WordPress service, I found the Sharp Serialization Library works well.

Basic usage for my helper is:

var browserInfo = new HappyBrowsingHelper().GetBrowserInfo(Request.UserAgent);

The results from the web service get stored in a model object called BrowserInfo:

public class BrowserInfo
  public string Name { get; set; }
  public string Version { get; set; }
  public string Url { get; set; }
  public string ImageUrl { get; set; }
  public string CurrentVersion { get; set; }
  public bool HasUpgrade { get; set; }
  public bool IsInsecure { get; set; }

The helper method to make the service call is as follows:

public BrowserInfo GetBrowserInfo(string userAgent)
  if (string.IsNullOrWhiteSpace(userAgent))
    return new BrowserInfo();

  var key = "__GetBrowserInfo_" + userAgent;
  var item = BiaCache.Get<BrowserInfo>(key);
  if (item == null)
    string serializedResponse = null;
      var postData = "useragent=" + userAgent;

      var request = WebRequest.Create("");
      request.Method = "POST";
      request.ContentType = "application/x-www-form-urlencoded";
      request.ContentLength = postData.Length;
      using (var writeStream = request.GetRequestStream())
        var encoding = new UTF8Encoding();
        var bytes = encoding.GetBytes(postData);
        writeStream.Write(bytes, 0, bytes.Length);

      using (var response = request.GetResponse())
        using (var responseStream = response.GetResponseStream())
          using (var reader = new StreamReader(responseStream, Encoding.UTF8))
            serializedResponse = reader.ReadToEnd();

      var serializer = new PhpSerializer();
      var result = (Hashtable) serializer.Deserialize(serializedResponse);
      item = new BrowserInfo
              Name = ToStringOrNull(result["name"]),
              Version = ToStringOrNull(result["version"]),
              Url = ToStringOrNull(result["update_url"]),
              ImageUrl = ToStringOrNull(result["img_src_ssl"]),
              CurrentVersion = ToStringOrNull(result["current_version"]),
              HasUpgrade = (bool) result["upgrade"],
              IsInsecure = (bool) result["insecure"]

      BiaCache.Add(key, item, (int) TimeSpan.FromDays(7).TotalMinutes, System.Web.Caching.CacheItemPriority.AboveNormal);
    catch (Exception ex)
      Log.Fatal("Error getting browser info from wordpress :( nUser agent: " + userAgent + "nResult: " + (serializedResponse ?? string.Empty) + "nn" + ex, ex);
      item = new BrowserInfo();
  return item;

private static string ToStringOrNull(object o)
  return o == null ? null : o.ToString();

Thats basically about it…  Happy browsing!  You can download a demo project: HappyBrowsingDemo.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.