Today while writing up a small blog module for one of my project, we were to implement a blog post view count feature where in on every blog post view we were to increment its view count field value by one.
Now one check we needed to place before registering a blog post’s view count was that the request that we had received was from a valid browser agent and not from any crawler, spider etc. This would help us get a better and legitimate view count for any blog post.
For accomplishing this task of identifying and validating a http request to be coming from a valid browser agent, not much direct help is available on google.
For the people who are in search of only the SOLUTION, here it goes:
Note: Below solution is in the form of an extension method that can be directly used with your Request object [e.g. Request.IsFromBrowser()]. But if you are using .Net framework version older then 3.0, which does not support extension methods, then remove the “this” keyword at the starting of argument, and call the function by passing your Request object.
/// <summary> /// Specifies whether a request has been made from a valid browser, /// and not from other sources like crawlers, spiders etc. /// </summary> /// <param name="objRequest">Current Request object</param> /// <returns>true is request is from a browser, else false</returns> public static bool IsFromBrowser(this HttpRequestBase objRequest) { return !objRequest.Browser.Crawler && objRequest.Browser.IsBrowser(objRequest.Browser.Browser); }
Now if you are interested in UNDERSTANDING the solution, please continue reading further:
All information related to our task is available in the user-agent string that we receive in the http request. But dont worry about parsing it out and matching up values with valid browser value sets. In DotNet framework, Microsoft has done the hard work of parsing these user agent strings in its HttpRequestBase class. It has also implemented a very efficient and extendable browser detection method where in it has stored browser agent information for various available browsers currently out there in market under {Windows}\Microsoft.NET\Framework\{Framework version}\Config\Browsers folder. Here in you would find various files with “.browser” extension. These are xml files with that browser’s user agent string’s parsing information. DotNet framework uses these as base information to parse current HttpRequest’s user agent string.
If you know of any browser agent that is not covered under these folder, you can create a “.browser” file for this new browser agent and add it under the above specified “browser” folder. Once this is done the dotNet framework’s IsBrowser function would detect this new user agent string file as a valid browser.
Note: If you want to get more information about the browser that has sent the http request (e.g. major/minor version; capabilities like css, javascript support, is touch enabled, can send emails, have cookies enabled; etc.), you would find it under various properties of Request.Browser object.
Happy Browser Detection !!!