Here’s the ASP.NET code for this utility:
This class gathers browser capability information, adds them to a list, and binds to a gridview control.
public partial class browserinfo : System.Web.UI.Page
{
List interestingItems = new List();
protected void btnInfo_Click1(object sender, EventArgs e)
{
// List of items to put at top because they're more interesting
interestingItems.Add("type");
interestingItems.Add("browser");
interestingItems.Add("win32");
interestingItems.Add("version");
interestingItems.Add("platform");
interestingItems.Add("extra");
interestingItems.Add("inputType");
System.Web.HttpBrowserCapabilities browser = Request.Browser;
List browserItemList = new List();
string sLocation = string.Empty;
try
{
// Get location from the IP browser address using a JSON web service
Location myLocation = new Location(Request);
// Create items for location information and add to list
browserItemList.Add(new BrowserInfoItem(" Source URL ", Request.UrlReferrer.ToString(), 1));
browserItemList.Add(new BrowserInfoItem(" Host Address ", Request.UserHostAddress, 2));
browserItemList.Add(new BrowserInfoItem(" Country Code ", myLocation._oLocation.RegionCode, 3));
browserItemList.Add(new BrowserInfoItem(" Region ", myLocation._oLocation.RegionName, 4));
browserItemList.Add(new BrowserInfoItem(" Approximate City ", myLocation._oLocation.City, 5));
browserItemList.Add(new BrowserInfoItem(" Approximate Latitude/Longitude ", myLocation._oLocation.Latitude + ", " + myLocation._oLocation.Longitude, 6));
}
catch (Exception ex)
{
// error handling here
}
// Loop through each item in browser capabilities and get the key and value
foreach (System.Collections.DictionaryEntry de in browser.Capabilities)
{
try
{
BrowserInfoItem bi;
if (interestingItems.Contains(de.Key.ToString()))
{
// Put interesting items at top
bi = new BrowserInfoItem(de.Key.ToString(), de.Value.ToString(), 10 );
}
else
{
bi = new BrowserInfoItem(de.Key.ToString(), de.Value.ToString(), 20 );
}
// add item to list
browserItemList.Add(bi);
}
catch
{
// Add empty string for null items, put at bottom
BrowserInfoItem bi = new BrowserInfoItem( de.Key.ToString() , "", 30);
browserItemList.Add(bi);
}
}
// Order so interesting items are at top
var myList = from itemList in browserItemList
orderby itemList.Priority ascending
select itemList;
// Bind list to gridview control and display results
gvInfo.DataSource = myList;
gvInfo.DataBind();
}
}