----INDEX---- <?xml version="1.0" encoding="utf-8"?> Soa"<soap:Envelope xmlns:xsi="&CHR(34)&""xmlns:xsd="&CHR(34)&""xmlns:soap="&CHR(34)&""<soap:Body>"& _ "<LoginByAccount xmlns="&CHR(34)&""<username>"&username&"</username>"& _ "</soap:Body>"& _ Set xmlhttp = server.CreateObject("Msxml2.XMLHTTP") <?xml version="1.0" encoding="utf-8"?> Set xmlDOC =server.CreateObject("MSXML.DOMDocument") Response.Write xmlhttp.Status&" " End if Set xmlDOC = server.CreateObject("MSXML.DOMDocument") Else Response.Write xmlhttp.Status&" " 顯示某節點各個屬性和數據的FUNCTION: Function showallnode(rootname,myxmlDOC)'望大家不斷完鄯 2005-1-9 writed by 844 set nodeobj=myxmlDOC.documentElement.selectSingleNode("http://"&rootname&"")'當前結點對像 returnstring=returnstring&"<BR>節點名稱:"&rootname if nodeobj.text<>"" then returnstring=returnstring&"<BR>{<BR>" if nodeAttributelen<>0 then for i=0 to nodeAttributelen-1 if nodeobj.childNodes.Length<>0 then returnstring=returnstring&"<BR>}<BR>" Set xmlDOC = server.CreateObject("MSXML.DOMDocument") Else Response.Write xmlhttp.Status&" " End if 二.POST請求示例 username=string&password=string Set xmlhttp = server.CreateObject("Msxml2.XMLHTTP") xmlhttp.Send(SoapRequest) <?xml version="1.0" encoding="utf-8"?> Set xmlDOC = server.CreateObject("MSXML.DOMDocument") Else Response.Write xmlhttp.Status&" " End if
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: "
<soap:Envelope xmlns:xsi=" <soap:Body>
<LoginByAccount xmlns=" <username>string</username>
<passWord>string</password>
</LoginByAccount>
</soap:Body>
</soap:Envelope>
為了與WEBSERVICE交互,需要構造一個與上完全相同的SOAP請求:
<%
url = "
"<password>"&password&"</password>"& _
"</LoginByAccount>"& _
"</soap:Envelope>"
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", "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
<soap:Envelope xmlns:xsi=" <soap:Body>
<LoginByAccountResponse xmlns=" <LoginByAccountResult>string</LoginByAccountResult>
</LoginByAccountResponse>
</soap:Body>
</soap:Envelope>
這是與剛才SOAP請求示例所對應的SOAP響應示例,在成功發送請求后,就可以查看該響應 :
If xmlhttp.Status = 200 Then
xmlDOC.load(xmlhttp.responseXML)
xmlStr = xmlDOC.xml
Set xmlDOC=nothing
xmlStr = Replace(xmlStr,"<","<")
xmlStr = Replace(xmlStr,">",">")
Response.write xmlStr
Else
Response.Write xmlhttp.StatusText
請求正確則給出完整響應,請求不正確(如賬號,密碼不對)響應的內容就會信息不完整.
取出響應里的數據,如下:
If xmlhttp.Status = 200 Then
xmlDOC.load(xmlhttp.responseXML)
Response.Write xmlDOC.documentElement.selectNodes("http://LoginByAccountResult")(0).text ‘顯示節點為LoginByAccountResult的數據(有編碼則要解碼)
Set xmlDOC = nothing
Response.Write xmlhttp.StatusText
End if
if rootname<>"" then
nodeAttributelen=myxmlDOC.documentElement.selectSingleNode("http://"&rootname&"").attributes.length'當前結點屬性數
returnstring=returnstring&"<BR>節點的文本:("&nodeobj.text&")"
end if
returnstring=returnstring&"<BR>屬性數有 "&nodeAttributelen&" 個,分別是:"
end if
returnstring=returnstring&"<li>"&nodeobj.attributes(i).Name&": "&nodeobj.getAttribute(nodeobj.attributes(i).Name)&" </li>"
next
if nodeobj.hasChildNodes() and lcase(nodeobj.childNodes.item(0).nodeName)<>"#text" then'是否有子節點
set childnodeobj=nodeobj.childNodes
childnodelen=nodeobj.childNodes.Length
returnstring=returnstring&"<BR><BR>有 "&childnodelen&" 個子節點;<BR>分別是: "
for i=0 to childnodelen-1
returnstring=returnstring&"<li>"&childnodeobj.item(i).nodeName&"</li>"
next
end if
end if
response.write returnstring
set nodeobj=nothing
end if
End Function
可以這樣用:
If xmlhttp.Status = 200 Then
xmlDOC.load(xmlhttp.responseXML)
showallnode "LoginByAccountResponse",xmlDOC’調用SHOWALLNODE
Set xmlDOC = nothing
Response.Write xmlhttp.StatusText
HTTP POST
下面是一個 HTTP POST 請求示例。所顯示的占位符需要由實際值替換。
POST /WebService1/UserSignOn.asmx/LoginByAccount HTTP/1.1
Host: 192.100.100.81
Content-Type: application/x-www-form-urlencoded
Content-Length: length
構造POST請求:
<%
url = "
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成功發送了與HTTP POST示例所符的POST請求.
‘檢測一下是否成功:
Response.Write xmlhttp.Status&” ”
Response.Write xmlhttp.StatusText
Set xmlhttp = Nothing
%>
如果成功會顯示200 ok,不成功會顯示 500 內部服務器錯誤〿 Connection: keep-alive .
成功后就可以利用WEBSERVICE的響應,如下:
HTTP POST
下面是一個 HTTP POST 響應示例。所顯示的占位符需要由實際值替換。
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<string xmlns=">
顯示:
If xmlhttp.Status = 200 Then
xmlDOC.load(xmlhttp.responseXML)
showallnode "string",xmlDOC'調用SHOWALLNODE
Set xmlDOC = nothing
Response.Write xmlhttp.StatusText
以上是asp用XMLHTTP組件發送SOAP請求,調用WEBSERVICE的方法,本人推薦在ASP環境下使用第一種方法,如果有更好的方法請聯系本人mailto:lyq8442002@msn.com .使用HTTP GET的方式如果有中文會出問題,數據量又不大。用HTTP POST的方法感覺多此一舉,其實上面的例子就是用POST的方式,只不過不是用POST的請求。用SOAP TOOLKIT要裝軟件,而且已沒有后繼版本。---全文完
新聞熱點
疑難解答