無需.net和專用組件實現(xiàn)用asp訪問webservice
2024-07-10 13:01:50
供稿:網(wǎng)友
可能,大多數(shù)的人認為我們需要運行asp.net或使用soap toolkit以訪問webservice
但是這不是必需的,使用微軟的xml parser我們同樣可以利用傳統(tǒng)的asp頁面來訪問webservice,下面我就展示給大家看一看!
我將使用三個文件來實現(xiàn)我的展示。
global.asa,當程序開始運行時,使用application變量
i_soapcall.asp 一個包含文件,用以訪問soap service
default.asp 一個基本的asp文件,用以顯示soap數(shù)據(jù)
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第一次執(zhí)行時,我們定義了一個變量:aspnetresources,它的值是函數(shù)getaspnetresources()的執(zhí)行結(jié)果,這
個函數(shù)可以在包含文件i_soapcall.asp中找到,他返回一個字符串,隨后我們定義了過期時間的變量:
application("aspnetexpires"),我們將在default.asp中使用,最后我們檢查了getaspnetresources()是否執(zhí)行正確,返回
值長度是否大于0,如果成功我們需要紀錄時間 application("aspnetresourcesupdated"), 和執(zhí)行結(jié)果
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")
%>
———————————————————————————————————————
現(xiàn)在是神秘的i_soapcall.asp
大家在想神秘的getaspnetresources()到底是什么樣子的,可以用基本的asp頁面調(diào)用webservice,不要忘了soap service無
論是wsdl文檔還是執(zhí)行結(jié)果都是一個xml文檔,所以我們可以parse(解析)它,這并不困難!
在函數(shù)中我們用到兩個object
function getaspnetresources()
set soaprequest = server.createobject("msxml2.xmlhttp")
set myxml =server.createobject("msxml.domdocument")
soaprequest 是服務(wù)器端組件,可以發(fā)送post和get請求。
myxml將被用來創(chuàng)建一個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
—————————————————————————————————————————————
我們設(shè)定soapurl 為我們的webservice的url然后我們用下面的語句打開連接
——————————————————————————————————————————————
soaprequest.open "get",soapurl , false
_____________________________________________________________________________________________
soaprequest.open有五個參數(shù),但是只有前兩個是必需的,這意味著其他三個是可以隨意選擇的
參數(shù)解釋:
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
設(shè)置完畢我們用soaprequest.send()向服務(wù)器發(fā)送請求,服務(wù)器返回的結(jié)果作為文本被存儲在soaprequest.responsexml中
我們要做的就是構(gòu)析這段文本。
下面給出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>
同樣的創(chuàng)作思路可以用在別的編程語言中