仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1400|回复: 18
打印 上一主题 下一主题

[学习教程] ASP编程:ASP若何挪用webservice

[复制链接]
爱飞 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-3 23:28:32 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
由于ASP提供的是一对多的服务,所以用户的一些特殊需求很难得到满足。   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 = WordStr(xmlStr,"<","< ")
xmlStr = WordStr(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
无法实现跨操作系统的应用。当然这也是微软的理由之一,只有这样才能发挥ASP最佳的能力。可是我却认为正是Windows限制了ASP,ASP的概念本就是为一个能让系统运行于一个大的多样化环境而设计的;
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-10 04:56

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表