可能大多数的人认为我们需要运行或使用soap toolkit以访问webservice但是这不是必需的使用微软的xml parser我们同样可以利用传统的asp页面来访问webservice下面我就展示给大家看一看!
我将使用三个文件来实现我的展示
globalasa当程序开始运行时使用application变量
i_soapcallasp 一个包含文件用以访问soap service
defaultasp 一个基本的asp文件用以显示soap数据Globalasa
当website运行时globalasa时刻都在运行在application_onstart中我们加入application变量
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
Dim ASPNETResources
ASPNETResources = GetASPNetResources()
Application(ASPNETExpires) =
If Len(ASPNETResources) > then
ApplicationLock
Application(ASPNETResourcesUpdated)=Now()
Application(ASPNETResourceList)=ASPNETResources
ApplicationUnLock
End if
End Sub
</script>
<! #include file=i_soapcallasp >
当application第一次执行时我们定义了一个变量ASPNETResources它的值是函数GetASPNetResources()的执行结果这个函数可以在包含文件i_soapcallasp中找到他返回一个字符串随后我们定义了过期时间的变量
Application(ASPNETExpires)我们将在defaultasp中使用最后我们检查了GetASPNetResources()是否执行正确返回值长度是否大于如果成功我们需要纪录时间 Application(ASPNETResourcesUpdated) 和执行结果Application(ASPNETResourceList) 在后面我将告诉大家这些变量是做什么的!
Defaultasp
defaultasp是用来显示我们的webservice请求
<%
Dim ASPNETResources
If len( Application(ASPNETResourceList) )> then
If DateDiff(hNow()Application(ASPNETResourcesUpdated)) > Application(ASPNETExpires) Then
ASPNETResources = GetASPNetResources()
ApplicationLock
Application(ASPNETResourcesUpdated)=Now()
Application(ASPNETResourceList)=ASPNETResources
ApplicationUnLock
End if
Else
ASPNETResources = GetASPNetResources()
ApplicationLock
Application(ASPNETResourcesUpdated)=Now()
Application(ASPNETResourceList)=ASPNETResources
ApplicationUnLock
End if
ResponseWrite Application(ASPNETResourceList)
%>
现在是神秘的i_soapcallasp
大家在想神秘的GetASPNetResources()到底是什么样子的可以用基本的asp页面调用webservice不要忘了soap service无论是wsdl文档还是执行结果都是一个xml文档所以我们可以parse(解析)它这并不困难!
在函数中我们用到两个object
Function GetASPNetResources()
Set SoapRequest = ServerCreateObject(MSXMLXMLHTTP)
Set myXML =ServerCreateObject(MSXMLDOMDocument)
SoapRequest 是服务器端组件可以发送post和get请求
myXML将被用来创建一个soap service的xml文档
myXMLAsync=False
SoapURL = _dsasmx/GetNewaspXResources?
SoapRequestOpen GETSoapURL False
SoapRequestSend()
if Not myXMLload(SoapRequestresponseXML) then
returnString =
Else
我们设定SoapURL 为我们的webservice的url然后我们用下面的语句打开连接SoapRequestOpen GETSoapURL False
SoapRequestOpen有五个参数但是只有前两个是必需的这意味着其他三个是可以随意选择的
参数解释
oServerXMLHTTPRequestopen 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
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
设置完毕我们用SoapRequestSend()向服务器发送请求服务器返回的结果作为文本被存储在SoapRequestresponseXML中我们要做的就是构析这段文本
下面给出i_soapcallasp的全部代码
<script language=vbscript runat=server>
Function GetASPNetResources()
Dim returnString
Dim myXML
Dim SoapRequest
Dim SoapURL
Set SoapRequest = ServerCreateObject(MSXMLXMLHTTP)
Set myXML =ServerCreateObject(MSXMLDOMDocument)
myXMLAsync=False
SoapURL = _dsasmx/GetNewaspXResources?
这是真实可用的webservice
SoapRequestOpen GETSoapURL False
SoapRequestSend()
if Not myXMLload(SoapRequestresponseXML) then an Error loading XML
returnString =
Elseparse 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=myXMLdocumentElementselectNodes(//URL)
Set nodesName=myXMLdocumentElementselectNodes(//Name)
REM uncomment the following lines if we want to access the DataUpdated and the Domain Nodes
REM Set nodesDateUpdated = myXMLdocumentElementselectNodes(//DateUpdated)
REM Set nodesDomain = myXMLdocumentElementselectNodes(//Domain)
REM the number of nodes in the list
NumOfNodes = nodesURLLength
ResourceList = <font face=verdana size=>Latest ASPNET Resources</font><ul>
For i = to NumOfNodes
ResourceList = ResourceList & <li><a & nodesURL(i)text & ><font face=verdana size=> &
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>
同样的创作思路可以用在别的编程语言中