ASP 呼叫WEB SERVICE 文件
----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"
為了與WEBSERVICE互動,需要構造一個與上完全相同的SOAP請求:
url = "http://192.100.100.81/WebService1/UserSignOn.asmx"
SoapRequest=""& _
"
"xmlns:xsd="&CHR(34)&"http://www.w3.org/2001/XMLSchema"&CHR(34)&" "& _
"xmlns:soap="&CHR(34)&"http://schemas.xmlsoap.org/soap/envelope/"&CHR(34)&">"& _
""& _
"
"
"
"
""& _
""
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&” ”
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請求示例所對應的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,"xmlStr = Replace(xmlStr,">",">")
Response.write xmlStr
Else
Response.Write xmlhttp.Status&" "
Response.Write xmlhttp.StatusText
End if
請求正確則給出完整響應,請求不正確(如賬號,密碼不對)響應的內容就會資訊不完整.
取出響應裡的資料,如下:
If xmlhttp.Status = 200 Then
Set xmlDOC = server.CreateObject("MSXML.DOMDocument")
xmlDOC.load(xmlhttp.responseXML)
Response.Write xmlDOC.documentElement.selectNodes("//LoginByAccountResult")(0).text ‘顯示節點為LoginByAccountResult的資料(有編碼則要解碼)
Set xmlDOC = nothing
Else
Response.Write xmlhttp.Status&" "
Response.Write xmlhttp.StatusText
End if
顯示某節點各個屬性和資料的FUNCTION:
Function showallnode(rootname,myxmlDOC)'望大家不斷完鄯 2005-1-9 writed by 844
if rootname<>"" then
set nodeobj=myxmlDOC.documentElement.selectSingleNode("//"&rootname&"")'當前結點對像
nodeAttributelen=myxmlDOC.documentElement.selectSingleNode("//"&rootname&"").attributes.length'當前結點屬性數
returnstring=returnstring&"
節點名稱:"&rootname
if nodeobj.text<>"" then
returnstring=returnstring&"
節點的文字:("&nodeobj.text&")"
end if
returnstring=returnstring&"
{
"
if nodeAttributelen<>0 then
returnstring=returnstring&"
屬性數有 "&nodeAttributelen&" 個,分別是:"
end if
for i=0 to nodeAttributelen-1
returnstring=returnstring&"
next
if nodeobj.childNodes.Length<>0 then
if nodeobj.hasChildNodes() and lcase(nodeobj.childNodes.item(0).nodeName)<>"#text" then'是否有子節點
set childnodeobj=nodeobj.childNodes
childnodelen=nodeobj.childNodes.Length
returnstring=returnstring&"
有 "&childnodelen&" 個子節點;
分別是: "
for i=0 to childnodelen-1
returnstring=returnstring&"
next
end if
end if
returnstring=returnstring&"
}
"
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’呼叫SHOWALLNODE
Set xmlDOC = nothing
Else
Response.Write xmlhttp.Status&" "
Response.Write xmlhttp.StatusText
End if
二.POST請求示例
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
username=string&password=string
構造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成功傳送了與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
顯示:
If xmlhttp.Status = 200 Then
Set xmlDOC = server.CreateObject("MSXML.DOMDocument")
xmlDOC.load(xmlhttp.responseXML)
showallnode "string",xmlDOC'呼叫SHOWALLNODE
Set xmlDOC = nothing
Else
Response.Write xmlhttp.Status&" "
Response.Write xmlhttp.StatusText
End if
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/14601556/viewspace-528538/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- c#呼叫web serviceC#Web
- iPhone 呼叫Web Service 例子(轉)iPhoneWeb
- 如何使用ABSL程式碼呼叫Web serviceWeb
- 翻譯-使用Spring呼叫SOAP Web ServiceSpringWeb
- web service實現原理與非同步呼叫Web非同步
- 什麼是web service?- SOAP Web Service & Restful Web ServiceWebREST
- ASP.NET AJAX中的非同步Web Services呼叫ASP.NET非同步Web
- asp.net web api 2 host in a windows service推薦閱讀ASP.NETWebAPIWindows
- 在Salesforce中向外公佈Service去建立Lead,並且用Asp.Net去呼叫此ServiceSalesforceASP.NET
- xml web serviceXMLWeb
- Web Service 教程Web
- 呼叫ASP.NET Web API不能傳送PUT/DELETE請求ASP.NETWebAPIdelete
- [ASP.NET State service] errorASP.NETError
- RESTful Web Service(續)RESTWeb
- Web Service 基礎Web
- Web Service入門Web
- Guava文件翻譯之 ServiceGuava
- ASP.NET Web API與Owin OAuth:呼叫與使用者相關的Web APIASP.NETWebAPIOAuth
- ASP.NET MVC4中呼叫WEB API的四個方法ASP.NETMVCWebAPI
- ASP.NET Web API 中使用 swagger 來管理 API 文件ASP.NETWebAPISwagger
- axis客戶端呼叫伺服器端方法,返回物件陣列(web service求助)客戶端伺服器物件陣列Web
- ASP.NET Web API 自定義MediaType實現jsonp跨域呼叫ASP.NETWebAPIJSON跨域
- 教你入門Web ServiceWeb
- Spring Web Service教程SpringWeb
- Web Service 的工作原理Web
- Web service是什麼?Web
- 研究Web Service同仁們,不知道對如何提高呼叫Web服務有什麼好的措施?Web
- 【Azure DevOps系列】使ASP.NET Core應用程式託管到Azure Web App ServicedevASP.NETWebAPP
- ASP 能都做到,在WEB中編輯WORD 文件!! JSP 如何實現?WebJS
- asp.net 呼叫 excel 元件ASP.NETExcel元件
- Android中的Web ServiceAndroidWeb
- Java:Web Service初入門JavaWeb
- Web Service 之 Python -- spyneWebPython
- Loadrunner11 --web serviceWeb
- web service相關轉摘Web
- c# web service轉貼C#Web
- 什麼是Web Service (轉)Web
- ASP.NET Web API與Owin OAuth:使用Access Toke呼叫受保護的APIASP.NETWebAPIOAuth