Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Detection for mobile device
Message
From
28/12/2015 16:15:17
 
 
To
All
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Detection for mobile device
Environment versions
Environment:
VB 9.0
OS:
Windows 8.1
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01629341
Message ID:
01629341
Views:
42
You probably have seen the popular code for detecting a mobile device from a .NET app. I have seen it is widely used by many developers. However, I haven't seen so far one who mentioned to make sure we have a user agent. Pretty much everyone assumes we always have one. If you use the HttpWebRequest object to send a Url somewhere. On that server, the user agent object would not be initialized.

So, the code is enhanbed like this:
    Public oRequest As System.Web.HttpRequest

    ' This assumes oRequest is initialized before coming here

		Dim laMobileType(66) As String

		cUserAgent = oRequest.UserAgent

		' If this is recognized at the object level
		If oBrowser.IsMobileDevice Then
			lMobileBrowser = True
		Else

			' If this is recognized at the WAP level
			If Not oRequest.ServerVariables("HTTP_X_WAP_PROFILE") Is Nothing Then
				lMobileBrowser = True
			Else

				' If the server variable exists
				If Not oRequest.ServerVariables("HTTP_ACCEPT") Is Nothing Then

					' If this is recognized at the WAP level at this verification
					If oRequest.ServerVariables("HTTP_ACCEPT").ToLower.Contains("wap") Then
						lMobileBrowser = True
					End If

				End If

				' If we have not recognized it yet
				If Not lMobileBrowser Then

					' If this is recognized at the user agent level
					If Not oRequest.ServerVariables("HTTP_X_WAP_PROFILE") Is Nothing Then
						lMobileBrowser = True
					Else

						' If we have a user agent object
						If Not cUserAgent Is Nothing Then

							' Initialization
							laMobileType(1) = "midp"
							laMobileType(2) = "j2me"
							laMobileType(3) = "avant"
							laMobileType(4) = "docomo"
							laMobileType(5) = "novarra"
							laMobileType(6) = "palmos"
							laMobileType(7) = "palmsource"
							laMobileType(8) = "240x320"
							laMobileType(9) = "opwv"
							laMobileType(10) = "chtml"
							laMobileType(11) = "pda"
							laMobileType(12) = "windows ce"
							laMobileType(13) = "mmp/"
							laMobileType(14) = "blackberry"
							laMobileType(15) = "mib/"
							laMobileType(16) = "symbian"
							laMobileType(17) = "wireless"
							laMobileType(18) = "nokia"
							laMobileType(19) = "hand"
							laMobileType(20) = "mobi"
							laMobileType(21) = "phone"
							laMobileType(22) = "cdm"
							laMobileType(23) = "up.b"
							laMobileType(24) = "audio"
							laMobileType(25) = "SIE-"
							laMobileType(26) = "SEC-"
							laMobileType(27) = "samsung"
							laMobileType(28) = "HTC"
							laMobileType(29) = "mot-"
							laMobileType(30) = "mitsu"
							laMobileType(31) = "sagem"
							laMobileType(32) = "sony"
							laMobileType(33) = "alcatel"
							laMobileType(34) = "lg"
							laMobileType(35) = "eric"
							laMobileType(36) = "vx"
							laMobileType(37) = "NEC"
							laMobileType(38) = "philips"
							laMobileType(39) = "mmm"
							laMobileType(40) = "xx"
							laMobileType(41) = "panasonic"
							laMobileType(42) = "sharp"
							laMobileType(43) = "wap"
							laMobileType(44) = "sch"
							laMobileType(45) = "rover"
							laMobileType(46) = "pocket"
							laMobileType(47) = "benq"
							laMobileType(48) = "java"
							laMobileType(49) = "pt"
							laMobileType(50) = "pg"
							laMobileType(51) = "vox"
							laMobileType(52) = "amoi"
							laMobileType(53) = "bird"
							laMobileType(54) = "compal"
							laMobileType(55) = "kg"
							laMobileType(56) = "voda"
							laMobileType(57) = "sany"
							laMobileType(58) = "kdd"
							laMobileType(59) = "dbt"
							laMobileType(60) = "sendo"
							laMobileType(61) = "sgh"
							laMobileType(62) = "gradi"
							laMobileType(63) = "jb"
							laMobileType(64) = "dddi"
							laMobileType(65) = "moto"
							laMobileType(66) = "iphone"

							' For each mobile type
							For lnCounter = 1 To 66

								' If this is one of them
								If cUserAgent.ToLower.Contains(laMobileType(lnCounter).ToLower) Then
									lMobileBrowser = True
								End If

							Next

						End If

					End If

				End If

			End If

		End If
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Next
Reply
Map
View

Click here to load this message in the networking platform