可能,大多数的人认为我们需要运行
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 = Get
ASPNetResources()
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,它的值是函数Get
ASPNetResources()的执行结果,这个函数可以在包含文件i_soapcall.
ASP中找到,他返回一个字符串,随后我们定义了过期时间的变量:
Application("
ASPNETExpires"),我们将在default.
ASP中使用,最后我们检查了Get
ASPNetResources()是否执行正确,返回值长度是否大于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 = Get
ASPNetResources()
Application.Lock
Application("
ASPNETResourcesUpdated")=Now()
Application("
ASPNETResourceList")=
ASPNETResources
Application.UnLock
End if
Else
ASPNETResources = Get
ASPNetResources()
Application.Lock
Application("
ASPNETResourcesUpdated")=Now()
Application("
ASPNETResourceList")=
ASPNETResources
Application.UnLock
End if
Response.Write Application("
ASPNETResourceList")
%>
现在是神秘的i_soapcall.
ASP 大家在想神秘的Get
ASPNetResources()到底是什么样子的,可以用基本的
ASP页面调用webservice,不要忘了soap service无论是wsdl文档还是执行结果都是一个
xml文档,所以我们可以parse(解析)它,这并不困难!
在函数中我们用到两个object
Function Get
ASPNetResources()
Set SoapRequest = Server.CreateObject("MS
xml2.
xmlHTTP")
Set my
xml =Server.CreateObject("MS
xml.DOMDocument")
SoapRequest 是
服务器端组件,可以发送post和get请求。
my
xml将被用来创建一个soap service的
xml文档
my
xml.Async=False
SoapURL = "http://64.85.12.73/WebSvc/whatsnew123apx_ds.asmx/GetNew123
ASPXResources?"
SoapRequest.Open "GET",SoapURL , False
SoapRequest.Send()
if Not my
xml.load(SoapRequest.response
xml) then
returnString = ""
Else
我们设定SoapURL 为我们的webservice的url然后我们用下面的语句打开连接SoapRequest.Open "GET",SoapURL , False
SoapRequest.Open有五个参数,但是只有前两个是必需的,这意味着其他三个是可以随意选择的
参数解释:
oServer
xmlHTTPRequest.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.response
xml中。我们要做的就是构析这段文本。
下面给出i_soapcall.
ASP的全部代码
<script language="vbscript" runat="server">
Function Get
ASPNetResources()
Dim returnString
Dim my
xml Dim SoapRequest
Dim SoapURL
Set SoapRequest = Server.CreateObject("MS
xml2.
xmlHTTP")
Set my
xml =Server.CreateObject("MS
xml.DOMDocument")
my
xml.Async=False
SoapURL = "http://64.85.12.73/WebSvc/whatsnew123apx_ds.asmx/GetNew123
ASPXResources?"
'这是真实可用的webservice
SoapRequest.Open "GET",SoapURL , False
SoapRequest.Send()
if Not my
xml.load(SoapRequest.response
xml) 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=my
xml.documentElement.selectNodes("//URL")
Set nodesName=my
xml.documentElement.selectNodes("//Name")
REM -- uncomment the following lines if we want to access the DataUpdated and the Domain Nodes
REM --Set nodesDateUpdated = my
xml.documentElement.selectNodes("//DateUpdated")
REM --Set nodesDomain = my
xml.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 my
xml = Nothing
Get
ASPNetResources = returnString
End Function
</script>
同样的创作思路可以用在别的编程语言中