• http://www.techneurons.com/career/
  • experienced programming consultants for hire !!!

    Contact Now

    ASP.NET Articles

    KB: Snippet: Get Browser Name from UserAgent String

    THURSDAY, FEBRUARY 11, 2010

    When you want to save logs on your website for any events like visits, errors, etc., it would be good if you add the browser name also. In case of errors, this information will do great for you, because client-side script errors and malfunctions might be browser dependant, and you would have to really debug them on those browsers in some cases.

    You can extract the browser name from the UserAgent string of the Request.UserAgent is a string which is used to identify a browser or a search bot or any kind of program to a server. This string will contain the browser keywords, version string, operating system and information on some of the browser plugins and system specifications. This is long string with length ranging from 20 to 200 and above characters.

    The UserAgent string will be a part of the HTTP Header of the request made to a server, and it will not be visible from the browser. But at the server, you can catch this UserAgent from the request header. In ASP.NET, Request.UserAgent will return the UserAgent string.

    The HTTP Header will look like this:

    HTTP_CONNECTION:Keep-Alive
    HTTP_ACCEPT:application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
    HTTP_ACCEPT_ENCODING:gzip, deflate
    HTTP_ACCEPT_LANGUAGE:en-IN
    HTTP_COOKIE:UserInteraction=KonaBase
    HTTP_HOST:whatsmyuseragent.com
    HTTP_REFERER:http://www.google.com/search?q=what+is+my+User+agent%3F&sourceid=ie7&rls=com.microsoft:en-in:IE-Address&ie=&oe=
    HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; Creative AutoUpdate v1.40.03) chromeframe/5.0.317.0

    The header contains many name-value pairs, and HTTP_USER_AGENT is one among them, which holds the UserAgent string. As you see in the above example, my UserAgent for Internet Explorer 8.0 from Windows 7 is:

    Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; Creative AutoUpdate v1.40.03) chromeframe/5.0.317.0

    The clause MSIE 8.0 in the above string says that this is an Internet Explorer 8.0 browser.

    http://www.whatsmyuseragent.com will tell you about the HTTP Header and UserAgent value of your browser

    The following VB.NET Snippet will help you to parse the Browser Name from the UserAgent string:

        Public Shared Function GetBrowserName() As String
            Dim userAgent As String = Request.UserAgent
            If userAgent.Contains("MSIE 5.0") Then
                Return "Internet Explorer 5.0"
            ElseIf userAgent.Contains("MSIE 6.0") Then
                Return "Internet Explorer 6.0"
            ElseIf userAgent.Contains("MSIE 7.0") Then
                Return "Internet Explorer 7.0"
            ElseIf userAgent.Contains("MSIE 8.0") Then
                Return "Internet Explorer 8.0"
            ElseIf userAgent.Contains("Firefox") Then
                Return userAgent.Substring(userAgent.IndexOf("Firefox")).Replace("/", " ")
            ElseIf userAgent.Contains("Opera") Then
                Return userAgent.Substring(userAgent.IndexOf("Opera"))
            ElseIf userAgent.Contains("Chrome") Then
                Dim agentPart As String = userAgent.Substring(userAgent.IndexOf("Chrome"))
                Return agentPart.Substring(0, agentPart.IndexOf("Safari") - 1).Replace("/", " ")
            ElseIf userAgent.Contains("Safari") Then
                Dim agentPart As String = userAgent.Substring(userAgent.IndexOf("Version"))
                Dim version As String = agentPart.Substring(0, agentPart.IndexOf("Safari") - 1).Replace("Version/", "")
                Return "Safari " & version
            ElseIf userAgent.Contains("Konqueror") Then
                Dim agentPart As String = userAgent.Substring(userAgent.IndexOf("Konqueror"))
                Return agentPart.Substring(0, agentPart.IndexOf(";")).Replace("/", " ")
            ElseIf userAgent.ToLower.Contains("bot") Or userAgent.ToLower.Contains("search") Then
                Return "Search Bot"
            End If
            Return "Unknown Browser or Bot"
        End Function

    You can add more browsers to this code, if you can research on more browsers and search engine bots. Hope this article will help you in recording and analyzing activities on your website.

    ConsultSarath - We provide end to end outsourcing solutions for .net programming requirements- you can hire programmer for hourly rates, for monthly commitments, for short term projects, for long term projects, Contact to know our hourly rates for programmer in India. ConsultSarath - We provide end to end outsourcing solutions for php programming requirements- you can hire programmer for hourly rates, for monthly commitments, for short term projects, for long term projects, Contact to know our hourly rates for programmer in India. ConsultSarath - We provide end to end outsourcing solutions for python programming requirements- you can hire programmer for hourly rates, for monthly commitments, for short term projects, for long term projects, Contact to know our hourly rates for programmer in India.

    Other Popular Articles
    We are experts in Cloud Computing Technologies. We can assist you to build high scalable business applications using Amazon Web Services (Amazon EC2, Amazon S3, Amazon SES, SNS, CloudFront), Windows Azure Platforms - Windows Azure and SQL Server Azure, Google App Engine using Python and Django Framework. We are Expert Programming Consultants available at affordable rates per hour. We work on several technologies - .NET, Python, Google App Engine, PHP, Windows Azure, Amazon Web Services ...