熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

不用.net和其組件用asp訪問webservice

2022-06-13   來源: ASP編程 

  可能大多數的人認為我們需要運行或使用soap toolkit以訪問webservice但是這不是必需的使用微軟的xml parser我們同樣可以利用傳統的asp頁面來訪問webservice下面我就展示給大家看一看!
  
  我將使用三個文件來實現我的展示
  
  globalasa當程序開始運行時使用application變量
  
  i_soapcallasp 一個包含文件用以訪問soap service
  
  defaultasp 一個基本的asp文件用以顯示soap數據Globalasa
  
  當website運行時globalasa時刻都在運行在application_onstart中我們加入application變量
  
  <SCRIPT LANGUAGE=VBScript RUNAT=Server>
  Sub Application_OnStart
  Dim ASPNETResources
  ASPNETResources = GetASPNetResources()
  Application(ASPNETExpires) =
  If Len(ASPNETResources) > then
  ApplicationLock
  Application(ASPNETResourcesUpdated)=Now()
  Application(ASPNETResourceList)=ASPNETResources
  ApplicationUnLock
  End if
  End Sub
  </script>
  <! #include file=i_soapcallasp >
  
  當application第一次執行時我們定義了一個變量ASPNETResources它的值是函數GetASPNetResources()的執行結果這個函數可以在包含文件i_soapcallasp中找到他返回一個字符串隨後我們定義了過期時間的變量
  
  Application(ASPNETExpires)我們將在defaultasp中使用最後我們檢查了GetASPNetResources()是否執行正確返回值長度是否大於如果成功我們需要紀錄時間 Application(ASPNETResourcesUpdated) 和執行結果Application(ASPNETResourceList) 在後面我將告訴大家這些變量是做什麼的!
  
  Defaultasp
  
  defaultasp是用來顯示我們的webservice請求
  
  <%
  Dim   ASPNETResources
  If len( Application(ASPNETResourceList) )> then
  If DateDiff(hNow()Application(ASPNETResourcesUpdated)) > Application(ASPNETExpires) Then
  ASPNETResources = GetASPNetResources()
  ApplicationLock
  Application(ASPNETResourcesUpdated)=Now()
  Application(ASPNETResourceList)=ASPNETResources
  ApplicationUnLock
  End if
  Else
  ASPNETResources = GetASPNetResources()
  ApplicationLock
  Application(ASPNETResourcesUpdated)=Now()
  Application(ASPNETResourceList)=ASPNETResources
  ApplicationUnLock
  End if
  ResponseWrite   Application(ASPNETResourceList)
  %>
  
  現在是神秘的i_soapcallasp
  
  大家在想神秘的GetASPNetResources()到底是什麼樣子的可以用基本的asp頁面調用webservice不要忘了soap service無論是wsdl文檔還是執行結果都是一個xml文檔所以我們可以parse(解析)它這並不困難!
  
  在函數中我們用到兩個object
  
  Function GetASPNetResources()
  Set SoapRequest = ServerCreateObject(MSXMLXMLHTTP)
  Set myXML =ServerCreateObject(MSXMLDOMDocument)
  
  SoapRequest 是服務器端組件可以發送post和get請求
  
  myXML將被用來創建一個soap service的xml文檔
  
  myXMLAsync=False
  SoapURL = _dsasmx/GetNewaspXResources?
  SoapRequestOpen GETSoapURL False
  SoapRequestSend()
  if Not myXMLload(SoapRequestresponseXML) then
  returnString =
  Else
  
  我們設定SoapURL 為我們的webservice的url然後我們用下面的語句打開連接SoapRequestOpen GETSoapURL False
  
  SoapRequestOpen有五個參數但是只有前兩個是必需的這意味著其他三個是可以隨意選擇的
  
  參數解釋
  
  oServerXMLHTTPRequestopen bstrMethod bstrUrl bAsync bstrUser bstrPassword
  Parameters
  bstrMethod
  HTTP method used to open the connection such as PUT or PROPFIND
  bstrUrl
  Requested URL This must be an absolute URL such as
  bAsync (optional)
  Boolean Indicator as to whether the call is asynchronous The default is False (the call does not
  return immediately)
  bstrUser (optional)
  Name of the user for authentication
  bstrPassword (optional)
  Password for authentication This parameter is ignored if the user parameter is Null or missing
  
  設置完畢我們用SoapRequestSend()向服務器發送請求服務器返回的結果作為文本被存儲在SoapRequestresponseXML中我們要做的就是構析這段文本
  
  下面給出i_soapcallasp的全部代碼
  
  <script language=vbscript runat=server>
  Function GetASPNetResources()
  Dim returnString
  Dim myXML
  Dim SoapRequest
  Dim SoapURL
  Set SoapRequest = ServerCreateObject(MSXMLXMLHTTP)
  Set myXML =ServerCreateObject(MSXMLDOMDocument)
  myXMLAsync=False
  SoapURL = _dsasmx/GetNewaspXResources?
  這是真實可用的webservice
  SoapRequestOpen GETSoapURL False
  SoapRequestSend()
  if Not myXMLload(SoapRequestresponseXML) then an Error loading XML
  returnString =
  Else  parse the XML
  Dim nodesURL
  Dim nodesName
  Dim nodesDateUpdated
  Dim nodesDomain
  Dim NumOfNodes
  Dim ResourceList
  Dim i
  REM The XML Nodes are CASE SENSITIVVE!
  Set nodesURL=myXMLdocumentElementselectNodes(//URL)
  Set nodesName=myXMLdocumentElementselectNodes(//Name)
  REM uncomment the following lines if we want to access the DataUpdated and the Domain Nodes
  REM Set nodesDateUpdated = myXMLdocumentElementselectNodes(//DateUpdated)
  REM Set nodesDomain = myXMLdocumentElementselectNodes(//Domain)
  REM the number of nodes in the list
  NumOfNodes = nodesURLLength
  ResourceList = <font face=verdana size=>Latest ASPNET Resources</font><ul>
  For i = to NumOfNodes
  ResourceList = ResourceList & <li><a & nodesURL(i)text & ><font face=verdana size=> &
  nodesName(i)text & </font></a></li>
  next
  ResourceList =ResourceList & </ul>
  returnString = ResourceList
  Set nodesURL = Nothing
  Set nodesName = Nothing
  End If
  Set SoapRequest = Nothing
  Set myXML = Nothing
  GetASPNetResources = returnString
  End Function
  </script>
  
  同樣的創作思路可以用在別的編程語言中
From:http://tw.wingwit.com/Article/program/ASP/201311/21871.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.