asp调用Web Service

首先发送请求,然后处理响应。我用.net写的WebService,直接访问,点击方法名称可以看到实例代码,只需要在asp中使用Microsoft.XMLHTTP发送请求,然后处理xml的结果就行了。

要注意Namespace不能写错了。

<%@language=vbscript codepage=936 %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body>
<%
Dim strxml
Dim str

'定义
soap消息
strxml = "<?xml version='1.0' encoding='tf-8'?>"
strxml = strxml & "<
soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns: soap='http://schemas.xml soap.org/ soap/envelope/'>"
strxml = strxml & "<
soap:Body> "
strxml = strxml & "<Decrypt xmlns='http://192.168.3.239:8000/3des/'>"
strxml = strxml & "<strIn>4Dv5esfHAh0=</strIn>"
strxml = strxml & "<strKey>Not.ceNte</strKey>"
strxml = strxml & "</Decrypt>"
strxml = strxml & "</
soap:Body>"
strxml = strxml & "</
soap:Envelope>"

Set h = createobject( "Microsoft.XMLHTTP")
'向指定的URL发送Post消息
h.open "POST", "http://192.168.3.239:8000/3des/Service.asmx", False
h.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
h.setRequestHeader "Content-Length",LEN(strxml)
h.setRequestHeader "
SOAPAction", "http://192.168.3.239:8000/3des/Decrypt"
h.send (strxml)

'显示返回的XML信息

If h.Status = 200 Then
   Set xmlDOC = server.CreateObject("MSXML.DOMDocument")
   xmlDOC.load(h.responseXML)
   str = xmlDOC.childNodes(1).Text    ''调用SHOWALLNODE

    '遍历并取出xml中的一个节点
    Set Node = xmlDOC.getElementsByTagName("RegionID")
    'for x=Node.length-1 to 0 step -1
    regMgrID = Node.item(0).Text
    'next

   Set xmlDOC = nothing
   response.write(str)   
Else
   Response.Write h.Status &"&nbsp;"
   Response.Write h.StatusText
End if

%>
</body>
</html>



转另外一个

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

你可能感兴趣的:(web Service)