ASP如何调用。Net的WebService

来源:互联网 发布:淘宝刷单一个月赚3000 编辑:程序博客网 时间:2024/04/28 19:03

ASP调用.Net的webservice的例子:
假设webservice的地址是http://192.168.0.88/test.asmx 接口是doAdd
下面是WebService的 SOAP 请求和响应示例
POST /test.asmx HTTP/1.1
Host: 192.168.0.88
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.test.com/doAdd"

<?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>
    <doAdd xmlns="http://www.test.com">
      <sRequest>string</sRequest>
    </doAdd>
  </soap:Body>
</soap:Envelope>
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>
    <doAddResponse xmlns="http://www.test.com">
      <doAddResult>string</doAddResult>
    </doAddResponse>
  </soap:Body>
</soap:Envelope>

下面是ASP页面:test.asp
<%
Dim url,xmlhttp,dom,node,xmlDOC
'根据webservice的测试页,构造不同的soap request
   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>"&_
    "<queryCardInfo xmlns="&CHR(34)&"http://tempuri.org/"&CHR(34)&">"&_
      "<nCardID>0</nCardID>"&_
      "<nAmt>1</nAmt>"&_
    "</queryCardInfo>"&_
  "</soap:Body>"&_
"</soap:Envelope>"
  
url = "http://192.168.0.88/test.asmx?op=doAdd"


Set xmlDOC =CreateObject("MSXML.DOMDocument")
xmlDOC.loadXML(SoapRequest)
Set xmlhttp = CreateObject("Msxml2.XMLHTTP")
xmlhttp.Open "POST",url,false
xmlhttp.setRequestHeader "Content-Type", "text/xml;charset=utf-8"
'SOAPAction这个Header头同样可以在sample中找到
xmlhttp.setRequestHeader "SOAPAction", "http://tempuri.org/queryCardInfo"
xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest)
xmlhttp.Send(xmlDOC)
response.write  "status = "&xmlhttp.Status&"<br>"
response.write  "statustxt = "&xmlhttp.StatusText&"<br>"
response.write  "responseText = "&xmlhttp.responseText&"<br>"
If xmlhttp.Status = 200 Then 'ok responseXML
 xmlDOC.load(xmlhttp.responseXML)
 'response.write  "price = "&xmlDOC.getElementsByTagName("PARAMS").item(0).xml&"<br>"
 response.Write(xmlDOC.xml)
else
 response.write  "failed"
end if
%>

原创粉丝点击