ASP使用xmlhttp调用WEBSERVICE文档
来源:CSDN 时间:2008-05-30 13:56:00
ASP调用WEBSERVICE
----INDEX----
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)
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
标签:xmlhttp,服务器,asp
0
投稿
猜你喜欢
ASP使用FSO组件生成HTML静态页面
2007-10-15 12:19:00
按钮上的内容
2007-11-27 00:13:00
ServerXMLHTTP的超时设置(setTimeouts)参数含义
2009-02-12 12:51:00
简单实用的图片播放器1.0(Javascript + css )
2008-07-16 10:39:00
什么是SVG(可升级矢量图形)
2008-05-06 12:37:00
在ASP中使用SQL语句之5:开始执行
2007-08-11 12:36:00
:hover在IE6下的问题
2009-06-18 21:09:00
JavaScript 颜色梯度和渐变效果
2009-03-18 11:16:00
用WEB(ASP)方式实现SQL SERVER 数据库的备份和恢复
2010-05-11 20:12:00
卓越网的配送服务让我很不满意
2009-03-19 13:49:00
Linux安装卸载Mysql数据库
2011-01-29 16:45:00
输入法下keyup失效的解决方案
2007-11-01 12:57:00
为导入文件加上时间戳标记的两种方法
2009-01-07 14:14:00
PHP实现表单处理方法详解
2023-05-25 07:39:18
ASP连接MySQL数据库代码示例
2010-03-14 11:24:00
全兼容可高亮二级缓冲折叠菜单
2010-06-03 16:53:00
使用access数据库时可能用到的数据转换
2008-09-10 12:49:00
JS:firefox 兼容性问题
2009-08-01 16:20:00
Web标准在中国
2008-11-26 11:27:00
玩转MySQL中的外键约束之PHP篇
2010-03-18 10:20:00