ASP如何调用webservice

作者:网络 来源:佚名 更新时间:2008-08-21 03:44:10 点击:
1. soap请求方式
2. post请求方式
3. showallnode函数(关于节点各属性和数据显示)

一.soap请求示例

  下面是一个 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

二.post请求示例
http post
下面是一个 http post 请求示例。所显示的占位符需要由实际值替换。
post /webservice1/usersignon.asmx/loginbyaccount http/1.1
host: 192.100.100.81
content-type: application/x-www-form-urlencoded
content-length: length

username=string&password=string
构造post请求:
<%
url = "http://192.100.100.81/webservice1/usersignon.asmx/loginbyaccount"

soaprequest="username="&username&"&password="&password

set xmlhttp = server.createobject("msxml2.xmlhttp")
xmlhttp.open "post",url,false
xmlhttp.setrequestheader "content-type", "application/x-www-form-urlencoded"’注意
xmlhttp.setrequestheader "host","192.100.100.81"
xmlhttp.setrequestheader "content-length",len(soaprequest)

xmlhttp.send(soaprequest)
‘这样就利用xmlhttp成功发送了与http post示例所符的post请求.
‘检测一下是否成功:
response.write xmlhttp.status&”  ”
response.write xmlhttp.statustext
set xmlhttp = nothing
%>
如果成功会显示200 ok,不成功会显示 500 内部服务器错误? connection: keep-alive .
成功后就可以利用webservice的响应,如下:
http post
下面是一个 http post 响应示例。所显示的占位符需要由实际值替换。
http/1.1 200 ok
content-type: text/xml; charset=utf-8
content-length: length

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">string</string>


显示:
if xmlhttp.status = 200 then

set xmldoc = server.createobject("msxml.domdocument")
xmldoc.load(xmlhttp.responsexml)
showallnode "string",xmldoc'调用showallnode
set xmldoc = nothing

else

response.write xmlhttp.status&"  "
response.write xmlhttp.statustext

end if