首先发送请求,然后处理响应。我用.net写的WebService,直接访问,点击方法名称可以看到实例代码,只需要在asp中使用Microsoft.XMLHTTP发送请求,然后处理xml的结果就行了。
要注意Namespace不能写错了。
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,">",">"