asp調用Web Service

來源:互聯網
上載者:User

首先發送請求,然後處理響應。我用.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.xmlsoap.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;"

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.