明輝手游網(wǎng)中心:是一個免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

ASP調(diào)用WEBSERVICE文檔

[摘要]----INDEX----1. soap請求方式2. post請求方式3. SHOWALLNODE函數(shù)(關(guān)于節(jié)點(diǎn)各屬性和數(shù)據(jù)顯示)---------------------一.SOAP請求示例下面是一個 SOAP 請求示例。所顯示的占位符需要由實(shí)際值替換。POST /WebService1/Use...

----INDEX----
1. soap請求方式
2. post請求方式
3. SHOWALLNODE函數(shù)(關(guān)于節(jié)點(diǎn)各屬性和數(shù)據(jù)顯示)
---------------------
一.SOAP請求示例
下面是一個 SOAP 請求示例。所顯示的占位符需要由實(shí)際值替換。
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交互,需要構(gòu)造一個與上完全相同的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的命名空間相同,否則服務(wù)會拒絕
xmlhttp.Send(SoapRequest)
‘這樣就利用XMLHTTP成功發(fā)送了與SOAP示例所符的SOAP請求.
‘檢測一下是否成功:
Response.Write xmlhttp.Status&”&nbsp;”
Response.Write xmlhttp.StatusText
Set xmlhttp = Nothing
%>
如果成功會顯示200 ok,不成功會顯示 500 內(nèi)部服務(wù)器錯誤〿 Connection: keep-alive .
成功后就可以利用WEBSERVICE的響應(yīng),如下:
SOAP響應(yīng)示例
下面是一個 SOAP 響應(yīng)示例。所顯示的占位符需要由實(shí)際值替換。
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請求示例所對應(yīng)的SOAP響應(yīng)示例,在成功發(fā)送請求后,就可以查看該響應(yīng) :
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
請求正確則給出完整響應(yīng),請求不正確(如賬號,密碼不對)響應(yīng)的內(nèi)容就會信息不完整.
取出響應(yīng)里的數(shù)據(jù),如下:
If xmlhttp.Status = 200 Then

Set xmlDOC = server.CreateObject("MSXML.DOMDocument")
xmlDOC.load(xmlhttp.responseXML)
Response.Write xmlDOC.documentElement.selectNodes("http://LoginByAccountResult")(0).text ‘顯示節(jié)點(diǎn)為LoginByAccountResult的數(shù)據(jù)(有編碼則要解碼)
Set xmlDOC = nothing

Else

Response.Write xmlhttp.Status&"&nbsp;"
Response.Write xmlhttp.StatusText


End if

顯示某節(jié)點(diǎn)各個屬性和數(shù)據(jù)的FUNCTION:

Function showallnode(rootname,myxmlDOC)'望大家不斷完鄯 2005-1-9 writed by 844
if rootname<>"" then

set nodeobj=myxmlDOC.documentElement.selectSingleNode("http://"&rootname&"")'當(dāng)前結(jié)點(diǎn)對像
nodeAttributelen=myxmlDOC.documentElement.selectSingleNode("http://"&rootname&"").attributes.length'當(dāng)前結(jié)點(diǎn)屬性數(shù)

returnstring=returnstring&"<BR>節(jié)點(diǎn)名稱:"&rootname

if nodeobj.text<>"" then
returnstring=returnstring&"<BR>節(jié)點(diǎn)的文本:("&nodeobj.text&")"
end if

returnstring=returnstring&"<BR>{<BR>"

if nodeAttributelen<>0 then
returnstring=returnstring&"<BR>屬性數(shù)有&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'是否有子節(jié)點(diǎn)
set childnodeobj=nodeobj.childNodes
childnodelen=nodeobj.childNodes.Length
returnstring=returnstring&"<BR><BR>有 "&childnodelen&" 個子節(jié)點(diǎn);<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’調(diào)用SHOWALLNODE
Set xmlDOC = nothing

Else

Response.Write xmlhttp.Status&"&nbsp;"
Response.Write xmlhttp.StatusText

End if

二.POST請求示例
HTTP POST
下面是一個 HTTP POST 請求示例。所顯示的占位符需要由實(shí)際值替換。
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
構(gòu)造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成功發(fā)送了與HTTP POST示例所符的POST請求.
‘檢測一下是否成功:
Response.Write xmlhttp.Status&”&nbsp;”
Response.Write xmlhttp.StatusText
Set xmlhttp = Nothing
%>
如果成功會顯示200 ok,不成功會顯示 500 內(nèi)部服務(wù)器錯誤〿 Connection: keep-alive .
成功后就可以利用WEBSERVICE的響應(yīng),如下:
HTTP POST
下面是一個 HTTP POST 響應(yīng)示例。所顯示的占位符需要由實(shí)際值替換。
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'調(diào)用SHOWALLNODE
Set xmlDOC = nothing

Else

Response.Write xmlhttp.Status&"&nbsp;"
Response.Write xmlhttp.StatusText

End if


以上是ASP用XMLHTTP組件發(fā)送SOAP請求,調(diào)用WEBSERVICE的方法,本人推薦在ASP環(huán)境下使用第一種方法,如果有更好的方法請聯(lián)系本人mailto:lyq8442002@msn.com .使用HTTP GET的方式如果有中文會出問題,數(shù)據(jù)量又不大。用HTTP POST的方法感覺多此一舉,其實(shí)上面的例子就是用POST的方式,只不過不是用POST的請求。用SOAP TOOLKIT要裝軟件,而且已沒有后繼版本。---全文完




相關(guān)文章