無需.net和專用組件實現用asp訪問webservice

來源:互聯網
上載者:User
web|訪問 可能,大多數的人認為我們需要運行asp.net或使用soap toolkit以訪問webservice
但是這不是必需的,使用微軟的xml parser我們同樣可以利用傳統的asp頁面來訪問webservice,下面我就展示給大家看一看!
我將使用三個檔案來實現我的展示。
    global.asa,當程式開始運行時,使用application變數
    i_soapcall.asp 一個包含檔案,用以訪問soap service
    default.asp 一個基本的asp檔案,用以顯示soap資料
Global.asa
        當website運行時global.asa時刻都在運行,在application_onstart中我們加入application變數
    原始碼:
————————————————————————————————————————————
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
    Dim ASPNETResources
    ASPNETResources = GetASPNetResources()    
    Application("ASPNETExpires") = 12    
    If Len(ASPNETResources) >0 then    
        Application.Lock
        Application("ASPNETResourcesUpdated")=Now()
        Application("ASPNETResourceList")=ASPNETResources
        Application.UnLock
    End if
End Sub
</script>
<!-- #include file="i_soapcall.asp" -->
——————————————————————————————————————
當application第一次執行時,我們定義了一個變數:ASPNETResources,它的值是函數GetASPNetResources()的執行結果,這

個函數可以在包含檔案i_soapcall.asp中找到,他返回一個字串,隨後我們定義了到期時間的變數:

Application("ASPNETExpires"),我們將在default.asp中使用,最後我們檢查了GetASPNetResources()是否執行正確,返回

值長度是否大於0,如果成功我們需要紀錄時間 Application("ASPNETResourcesUpdated"), 和執行結果

Application("ASPNETResourceList"). 在後面我將告訴大家這些變數是做什麼的!

Default.asp
default.asp是用來顯示我們的webservice請求
——————————————————————————————————————————————
<%
Dim     ASPNETResources    
If len( Application("ASPNETResourceList") )>0 then    
   
    If DateDiff("h",Now(),Application("ASPNETResourcesUpdated")) > Application("ASPNETExpires") Then    
        
        ASPNETResources = GetASPNetResources()
        Application.Lock
        Application("ASPNETResourcesUpdated")=Now()
        Application("ASPNETResourceList")=ASPNETResources
        Application.UnLock
    End if

Else   
    ASPNETResources = GetASPNetResources()
    Application.Lock
    Application("ASPNETResourcesUpdated")=Now()
    Application("ASPNETResourceList")=ASPNETResources
    Application.UnLock

End if
Response.Write     Application("ASPNETResourceList")
%>
———————————————————————————————————————
現在是神秘的i_soapcall.asp
大家在想神秘的GetASPNetResources()到底是什麼樣子的,可以用基本的asp頁面調用webservice,不要忘了soap service無

論是wsdl文檔還是執行結果都是一個xml文檔,所以我們可以parse(解析)它,這並不困難!
在函數中我們用到兩個object
Function GetASPNetResources()    
    Set SoapRequest = Server.CreateObject("MSXML2.XMLHTTP")
    Set myXML =Server.CreateObject("MSXML.DOMDocument")
SoapRequest 是伺服器端組件,可以發送post和get請求。
myXML將被用來建立一個soap service的xml文檔
————————————————————————————————————————————
myXML.Async=False
    SoapURL = "http://64.85.12.73/WebSvc/whatsnew123apx_ds.asmx/GetNew123aspXResources?"
    SoapRequest.Open "GET",SoapURL , False
    SoapRequest.Send()

    if Not myXML.load(SoapRequest.responseXML) then
        returnString = ""
    Else   
—————————————————————————————————————————————
我們設定SoapURL 為我們的webservice的url然後我們用下面的語句開啟串連
——————————————————————————————————————————————
SoapRequest.Open "GET",SoapURL , False
_____________________________________________________________________________________________
SoapRequest.Open有五個參數,但是只有前兩個是必需的,這意味著其他三個是可以隨意選擇的
參數解釋:
oServerXMLHTTPRequest.open bstrMethod, bstrUrl, bAsync, bstrUser, bstrPassword
    Parameters
    bstrMethod
        HTTP method used to open the connection, such as PUT or PROPFIND.
    bstrUrl
        Requested URL. This must be an absolute URL, such as "http://Myserver/Mypath/Myfile.asp".
    bAsync (optional)
        Boolean. Indicator as to whether the call is asynchronous. The default is False (the call does not

return immediately).
    bstrUser (optional)
        Name of the user for authentication.
    bstrPassword (optional)
        Password for authentication. This parameter is ignored if the user parameter is Null or missing
設定完畢我們用SoapRequest.Send()向伺服器發送請求,伺服器返回的結果作為文本被儲存在SoapRequest.responseXML中
我們要做的就是構析這段文本。
下面給出i_soapcall.asp的全部代碼
<script language="vbscript" runat="server">
Function GetASPNetResources()    
    Dim returnString
    Dim myXML
    Dim SoapRequest
    Dim SoapURL

    Set SoapRequest = Server.CreateObject("MSXML2.XMLHTTP")
    Set myXML =Server.CreateObject("MSXML.DOMDocument")

    myXML.Async=False
    SoapURL = "http://64.85.12.73/WebSvc/whatsnew123apx_ds.asmx/GetNew123aspXResources?"
    '這是真實可用的webservice
    SoapRequest.Open "GET",SoapURL , False
    SoapRequest.Send()

    if Not myXML.load(SoapRequest.responseXML) then 'an Error loading XML
        returnString = ""
    Else    'parse the XML

        Dim nodesURL
        Dim nodesName
        Dim nodesDateUpdated
        Dim nodesDomain
        Dim NumOfNodes
        Dim ResourceList
        Dim i

        REM -- The XML Nodes are CASE SENSITIVVE!
        Set nodesURL=myXML.documentElement.selectNodes("//URL")
        Set nodesName=myXML.documentElement.selectNodes("//Name")

        REM -- uncomment the following lines if we want to access the DataUpdated and the Domain Nodes
        REM --Set nodesDateUpdated = myXML.documentElement.selectNodes("//DateUpdated")
        REM --Set nodesDomain = myXML.documentElement.selectNodes("//Domain")

        REM -- the number of nodes in the list
        NumOfNodes = nodesURL.Length
        ResourceList = "<font face=verdana size=2>Latest ASP.NET Resources</font><ul>"

        For i = 0 to NumOfNodes -1
            ResourceList = ResourceList & "<li><a href=" & nodesURL(i).text & "><font face=verdana size=2>" &

nodesName(i).text & "</font></a></li>"
        next

        ResourceList =ResourceList & "</ul>"
        
        returnString = ResourceList
    
        Set nodesURL = Nothing
        Set nodesName = Nothing
    End If
    
    Set SoapRequest = Nothing
    Set myXML = Nothing
    
    
    GetASPNetResources = returnString
End Function
</script>
同樣的創作思路可以用在別的程式設計語言中

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.