65.9K
CodeProject 正在变化。 阅读更多。
Home

如何使用 VBScript 获取移动信息

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.86/5 (20投票s)

2009 年 5 月 28 日

CPOL
viewsIcon

35520

downloadIcon

371

一种简单的方法来获取移动设备型号、厂商、屏幕尺寸等。

引言

本文将向您展示如何使用简单的 VBScript 进行基本的用户代理/移动设备功能(移动设备型号、厂商等)分析。

背景

我创建了一个 WAP 应用程序,用于获取有关移动设备型号及其厂商的信息,并将其存储在简单的文本文件中。 最初,我决定使用 Java 创建一个应用程序,但问题是人们需要下载和安装该应用程序;最终,我决定创建一个简单的 WML 编码 ASP 页面。

使用代码

现在我们将讨论如何存储手机型号及其厂商。 首先,我想向您展示基本的 UAProfile 属性获取例程,我使用 Msxml2.dll,并且没有内部依赖关系。 示例代码如下

'Function GetValue(): used for HTTP POST/Get and XML parsing 

Function GetValue(ByVal url, ByVal name)

//###############################################
// Author: Md. Marufuzzaman
// Basic UapProfile attribute fetching routine
// Internal Dependencies: None
// !!! Mother of all UAP Routines
// ###############################################

    On Error Resume Next
    Dim http
    Dim document
    Dim namespaces
    Dim node
    Dim xpath

    Set http = Server.CreateObject("Msxml2.XMLHTTP")
    http.Open "GET", url, False
    http.Send
    
    
    If http.Status <> "200" Then
         GetValue = Nothing
         Exit Function
    End If

    Set document = Server.CreateObject("Msxml2.DOMDocument")
    document.async = False
    document.validateOnParse = False
    document.resolveExternals = False
    document.load http.responseBody

    If document.parseError.errorCode <> 0 Then
         GetValue = Nothing
         Exit Function
    End If
         
    document.setProperty "SelectionLanguage","XPath"
    xpath = "//*[local-name() = '" & name & "']"

    Set node = document.selectSingleNode(xpath)

    If Not node Is Nothing Then
         GetValue = node.Text
    Else
         GetValue = Nothing
    End If

    Set document = Nothing
    Set http = Nothing


End Function

'Function GetWidth(): Get mobile screen width

Function GetWidth()
//##################################################
// Author: Md. Marufuzzaman
// Gets the Screen Height of the Mobile screen 
// !!! Local Function Defendencies:              
// GetValue()                                 
// Revision:                                
//##################################################

    profile=replace(Request.ServerVariables("HTTP_PROFILE"),chr(34),"")
    xwapprofile=replace(Request.ServerVariables("HTTP_X_WAP_PROFILE"), _
                        chr(34),"")
    profileurl=    profile
    if profile= "" then
         vProf = split(xwapprofile,",")
         profileurl= vProf(0)
    end if
        
            
    //-----------------------------------------------------------
    retval = GetValue(profileurl,"ScreenSize")
    if retval <> "" then
        screenpara = split(retval,"x") //ok 
        GetWidth= screenpara(lbound(screenpara)) //OK
    else
        GetWidth= 144 //<------- the default width
    end if
    //-----------------------------------------------------------

End Function


'Function GetHeight(): Get mobile screen height

Function GetHeight()

//##################################################
// Author: Md. Marufuzzaman
// Gets the Screen Height of the Mobile screen //
// !!! Local Function Defendencies:              //
// GetValue()                                 //
// Revision:                                //
//###########################################
            
    profile=replace(Request.ServerVariables("HTTP_PROFILE"),chr(34),"")
    xwapprofile=replace(Request.ServerVariables("HTTP_X_WAP_PROFILE"), _
                        chr(34),"")
    profileurl=    profile
    if profile= "" then
         vProf = split(xwapprofile,",")
         profileurl= vProf(0)
    end if
    
    //-----------------------
    retval = GetValue(profileurl,"ScreenSize")
    
    if  retval <> "" then
        screenpara = split(retval,"x") //ok 
        GetHeight=screenpara(ubound(screenpara))
    else
        GetHeight= 176 //<---- the default height 
    end if
    //--------------

End Function

'Function Is_PDA(): Return true if the request from Windows PDA browser.

Function Is_PDA()

//###############################################
// Author: Md. Marufuzzaman
// Detect Windows CE/OS PDA
// // ###############################################

    Dim varUAProfile
    Dim varArray
    Dim variCounter
                            
    varUAProfile = Request.ServerVariables("HTTP_USER_AGENT")
    
    varUAProfile = varUAProfile & " #"    
    varArray = split(varUAProfile, " " )
        
    variCounter = 0

        while varArray(variCounter) <> "#"
            
            if ucase(varArray(variCounter)) = "WINDOWS" OR _
               ucase(varArray(variCounter+1)) = "CE" then

                Is_PDA = True
                Exit Function
            else
                Is_PDA = False
            end if
            
            variCounter = variCounter + 1
        wend
End Function

'Function HandSetMode(): Get the device model and the vendors 
'                        and store into a text file.

Function HandSetMode ()
//###########################################
// Author: Md. Marufuzzaman                   
// Revision:                               
//##########################################


  Dim fs,fname
  Dim varVendor,varHandSetModel,varData
  Dim varProf,varWapProfile
  Dim varProfile 
    
  varProfile=replace(Request.ServerVariables("HTTP_PROFILE"),chr(34),"")
  varWapProfile=replace(Request.ServerVariables("HTTP_X_WAP_PROFILE"),chr(34),"")

  profileurl = varProfile

    if varProfile= "" then
        varProf = split(varWapProfile,",")
          profileurl=varProf(0)
    end if        
    
    varVendor= ucase(GetValue(profileurl,"Vendor"))
    varHandSetModel= ucase(GetValue(profileurl,"Model"))
        
    varData = "Vendor:-" & varVendor & ", HandSet_Model # " & _
              varHandSetModel & ", UAProf:" & profileurl & _
              ", TimeStamp # " & Now()

    set fs=Server.CreateObject("Scripting.FileSystemObject")
    set fname=fs.OpenTextFile(Server.MapPath("HandSetList.txt"),8,true)
    // 8 = ForAppending

    fname.WriteLine(varData)

    fname.Close
    
    set fname=nothing
    set fs=nothing

End Function

带有 WML 编码的 ASP 页面示例代码

// Include the header file UAProfiling.in (VBScript files)

<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" 
   "http://www.wapforum.org/DTD/wml_1.1.xml">

<wml>
<card id="profileview" title="Mobile Capabilities">

<p align="center">

<%

' Call the VBScript function

%>

</p>
</card>
</wml>

在上面的函数 GetValue() 中,我创建了两个对象

  1. Msxml2.XMLHTTP,用于 HTTP GET 请求。
  2. Msxml2.DOMDocument,用于 XML 解析。

结论

我希望这能对您有所帮助。 祝您愉快!

© . All rights reserved.