可能,大多数的人认为我们需要运行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>
同样的创作思路可以用在别的编程语言中
#############################################
后面这个方法不错
下面是一个 soap 请求示例。所显示的占位符需要由实际值替换。
PosT /WebService1/UserSignOn.asmx HTTP/1.1
Host: 192.100.100.81
Content-Type: text/xml; charset=utf-8
Content-Length: length
soapAction: "http://tempuri.org/LoginByAccount"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/xmlSchema-instance" xmlns:xsd="http://www.w3.org/2001/xmlSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<LoginByAccount xmlns="http://tempuri.org/">
<username>string</username>
<password>string</password>
</LoginByAccount>
</soap:Body>
</soap:Envelope>
为了与WEBSERVICE交互,需要构造一个与上完全相同的soap请求:
<%
url = "http://192.100.100.81/WebService1/UserSignOn.asmx"
soapRequest="<?xml version="&CHR(34)&"1.0"&CHR(34)&" encoding="&CHR(34)&"utf-8"&CHR(34)&"?>"& _
"<soap:Envelope xmlns:xsi="&CHR(34)&"http://www.w3.org/2001/xmlSchema-instance"&CHR(34)&" "& _
"xmlns:xsd="&CHR(34)&"http://www.w3.org/2001/xmlSchema"&CHR(34)&" "& _
"xmlns:soap="&CHR(34)&"http://schemas.xmlsoap.org/soap/envelope/"&CHR(34)&">"& _
"<soap:Body>"& _
"<LoginByAccount xmlns="&CHR(34)&"http://tempuri.org/"&CHR(34)&">"& _
"<username>"&username&"</username>"& _
"<password>"&password&"</password>"& _
"</LoginByAccount>"& _
"</soap:Body>"& _
"</soap:Envelope>"
Set xmlhttp = server.CreateObject("Msxml2.xmlHTTP")
xmlhttp.Open "PosT",url,false
xmlhttp.setRequestHeader "Content-Type", "text/xml;charset=utf-8"
xmlhttp.setRequestHeader "HosT","192.100.100.81"
xmlhttp.setRequestHeader "Content-Length",LEN(soapRequest)
xmlhttp.setRequestHeader "soapAction", "http://tempuri.org/LoginByAccount" ‘一定要与WEBSERVICE的命名空间相同,否则服务会拒绝
xmlhttp.Send(soapRequest)
‘这样就利用xmlHTTP成功发送了与soap示例所符的soap请求.
‘检测一下是否成功:
Response.Write xmlhttp.Status&” ”
Response.Write xmlhttp.StatusText
Set xmlhttp = Nothing
%>
如果成功会显示200 ok,不成功会显示 500 内部服务器错误? Connection: keep-alive .
成功后就可以利用WEBSERVICE的响应,如下:
soap响应示例
下面是一个 soap 响应示例。所显示的占位符需要由实际值替换。
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/xmlSchema-instance" xmlns:xsd="http://www.w3.org/2001/xmlSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<LoginByAccountResponse xmlns="http://tempuri.org/">
<LoginByAccountResult>string</LoginByAccountResult>
</LoginByAccountResponse>
</soap:Body>
</soap:Envelope>
这是与刚才soap请求示例所对应的soap响应示例,在成功发送请求后,就可以查看该响应 :
If xmlhttp.Status = 200 Then
Set xmlDOC =server.CreateObject("MSxml.DOMDocument")
xmlDOC.load(xmlhttp.responsexml)
xmlStr = xmlDOC.xml
Set xmlDOC=nothing
xmlStr = Replace(xmlStr,"<","<")
xmlStr = Replace(xmlStr,">",">")
Response.write xmlStr
Else
Response.Write xmlhttp.Status&" "
Response.Write xmlhttp.StatusText
End if
请求正确则给出完整响应,请求不正确(如账号,密码不对)响应的内容就会信息不完整.
取出响应里的数据,如下:
If xmlhttp.Status = 200 Then
Set xmlDOC = server.CreateObject("MSxml.DOMDocument")
xmlDOC.load(xmlhttp.responsexml)
Response.Write xmlDOC.documentElement.selectNodes("//LoginByAccountResult")(0).text ‘显示节点为LoginByAccountResult的数据(有编码则要解码)
Set xmlDOC = nothing
Else
Response.Write xmlhttp.Status&" "
Response.Write xmlhttp.StatusText
End if
显示某节点各个属性和数据的FUNCTION:
Function showallnode(rootname,myxmlDOC)''望大家不断完鄯 2005-1-9 writed by 844
if rootname<>"" then
set nodeobj=myxmlDOC.documentElement.selectSingleNode("//"&rootname&"")''当前结点对像
nodeAttributelen=myxmlDOC.documentElement.selectSingleNode("//"&rootname&"").attributes.length''当前结点属性数
returnstring=returnstring&"<BR>节点名称:"&rootname
if nodeobj.text<>"" then
returnstring=returnstring&"<BR>节点的文本:("&nodeobj.text&")"
end if
returnstring=returnstring&"<BR>{<BR>"
if nodeAttributelen<>0 then
returnstring=returnstring&"<BR>属性数有 "&nodeAttributelen&" 个,分别是:"
end if
for i=0 to nodeAttributelen-1
returnstring=returnstring&"<li>"&nodeobj.attributes(i).Name&": "&nodeobj.getAttribute(nodeobj.attributes(i).Name)&" </li>"
next
if nodeobj.childNodes.Length<>0 then
if nodeobj.hasChildNodes() and lcase(nodeobj.childNodes.item(0).nodeName)<>"#text" then''是否有子节点
set childnodeobj=nodeobj.childNodes
childnodelen=nodeobj.childNodes.Length
returnstring=returnstring&"<BR><BR>有 "&childnodelen&" 个子节点;<BR>分别是: "
for i=0 to childnodelen-1
returnstring=returnstring&"<li>"&childnodeobj.item(i).nodeName&"</li>"
next
end if
end if
returnstring=returnstring&"<BR>}<BR>"
response.write returnstring
set nodeobj=nothing
end if
End Function
可以这样用:
If xmlhttp.Status = 200 Then
Set xmlDOC = server.CreateObject("MSxml.DOMDocument")
xmlDOC.load(xmlhttp.responsexml)
showallnode "LoginByAccountResponse",xmlDOC’调用SHOWALLNODE
Set xmlDOC = nothing
Else
Response.Write xmlhttp.Status&" "
Response.Write xmlhttp.StatusText
End if