无需.net和专用组件实现用asp访问webservice
2024-07-10 13:01:50
供稿:网友
可能,大多数的人认为我们需要运行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>
同样的创作思路可以用在别的编程语言中