欢迎光临
我们一直在努力

ASP调用WEBSERVICE文档-ASP教程,ASP应用

建站超值云服务器,限时71元/月

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&”&nbsp;”

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,"<","&lt;")

xmlstr = replace(xmlstr,">","&gt;")

response.write xmlstr

else

response.write xmlhttp.status&"&nbsp;"

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&"&nbsp;"

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>属性数有&nbsp; "&nodeattributelen&" 个,分别是:"

end if

for i=0 to nodeattributelen-1

returnstring=returnstring&"<li>"&nodeobj.attributes(i).name&":&nbsp;"&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&"&nbsp;"

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&”&nbsp;”

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&"&nbsp;"

response.write xmlhttp.statustext

end if

以上是asp用xmlhttp组件发送soap请求,调用webservice的方法,本人推荐在asp环境下使用第一种方法,如果有更好的方法请联系本人mailto:lyq8442002@msn.com .使用http get的方式如果有中文会出问题,数据量又不大。用http post的方法感觉多此一举,其实上面的例子就是用post的方式,只不过不是用post的请求。用soap toolkit要装软件,而且已没有后继版本。—全文完

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP调用WEBSERVICE文档-ASP教程,ASP应用
分享到: 更多 (0)