Use ASP to access WebService without using. NET and its components

Source: Internet
Author: User
Tags xml parser
Probably, most people think we need to run Asp.net or use soap toolkit to access WebService. But this is not necessary. We can also use the traditional ASP page to access WebService using Microsoft's XML parser. I will show you the following!
  
I will use three files for my presentation.
  
Global. Asa. When the program starts running, use the application variable
  
I _soapcall.asp is an inclusion file used to access the soap service.
  
Default. asp is a basic ASP file used to display the soap data global. asa
  
When the website is running, global. ASA is running at all times. In application_onstart, we add the application variable.
  
<Script language = VBScript runat = Server>
Sub application_onstart
Dim aspnetresources
Aspnetresources = getaspnetresources ()
Application ("aspnetexpires") = 12
If Len (aspnetresources)> 0 then
Application. Lock
Application ("aspnetresourcesupdated") = now ()
Application ("aspnetresourcelist") = aspnetresources
Application. Unlock
End if
End sub
</SCRIPT>
<! -- # Include file = "I _soapcall.asp" -->
  
When the application is executed for the first time, we define a variable: aspnetresources, whose value is the execution result of the getaspnetresources () function. This function can be found in the I _soapcall.asp file, it returns a string, and then we define the expiration time variable:
  
Application ("aspnetexpires"), we will. used in ASP. Finally, we check whether getaspnetresources () is correctly executed and whether the returned value length is greater than 0. If it succeeds, we need to record the time application ("aspnetresourcesupdated "), and the execution result application ("aspnetresourcelist "). in the future, I will tell you what these variables do!
  
Default. asp
  
Default. asp is used to display our WebService requests
  
<%
Dim aspnetresources
If Len (Application ("aspnetresourcelist")> 0 then
If datediff ("H", now (), application ("aspnetresourcesupdated")> application ("aspnetexpires") then
Aspnetresources = getaspnetresources ()
Application. Lock
Application ("aspnetresourcesupdated") = now ()
Application ("aspnetresourcelist") = aspnetresources
Application. Unlock
End if
Else
Aspnetresources = getaspnetresources ()
Application. Lock
Application ("aspnetresourcesupdated") = now ()
Application ("aspnetresourcelist") = aspnetresources
Application. Unlock
End if
Response. Write application ("aspnetresourcelist ")
%>
  
This is the mysterious I _soapcall.asp
  
You are wondering what the mysterious getaspnetresources () looks like. You can use the basic ASP page to call WebService. Do not forget that soap service is an XML document for both the WSDL document and the execution result, so we can parse it, which is not difficult!
  
In the function, we use two objects.
  
Function getaspnetresources ()
Set soaprequest = server. Createobject ("msxml2.xmlhttp ")
Set myxml = server. Createobject ("MSXML. domdocument ")
  
Soaprequest is a server component that can send post and GET requests.
  
Myxml will be used to create an XML document for the soap Service
  
Myxml. async = false
Soapurl = "http: // 64.85.12.73/websvc/whatsnew123apx_ds.asmx/getnew123aspxresources? "
Soaprequest. Open "get", soapurl, false
Soaprequest. Send ()
If not myxml. Load (soaprequest. responsexml) then
Returnstring = ""
Else
  
We set soapurl to the URL of our WebService, And then we use the following statement to open the connection soaprequest. Open "get", soapurl, false
  
Soaprequest. Open has five parameters, but only the first two are required. This means that the other three parameters can be selected at will.
  
Parameter description:
  
Oserverxmlhttprequest. Open 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 "http: // myserver/mypath/myfile. asp ".
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
  
After the settings are complete, we use soaprequest. Send () to send requests to the server. The results returned by the server are stored in soaprequest. responsexml as text. What we need to do is to analyze this text.
  
The following describes all the codes of I _soapcall.asp.
  
<Script language = "VBScript" runat = "server">
Function getaspnetresources ()
Dim returnstring
Dim myxml
Dim soaprequest
Dim soapurl
Set soaprequest = server. Createobject ("msxml2.xmlhttp ")
Set myxml = server. Createobject ("MSXML. domdocument ")
Myxml. async = false
Soapurl = "http: // 64.85.12.73/websvc/whatsnew123apx_ds.asmx/getnew123aspxresources? "
'This is a truly available WebService.
Soaprequest. Open "get", soapurl, false
Soaprequest. Send ()
If not myxml. Load (soaprequest. responsexml) 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 nodesurlnames myxml.doc umentelement. selectnodes ("// URL ")
Set nodesnamedes myxml.doc umentelement. selectnodes ("// name ")
Rem -- uncomment the following lines if we want to access the dataupdated and the Domain Nodes
Rem -- set nodesdateupdated = myxml.doc umentelement. selectnodes ("// dateupdated ")
Rem -- set nodesdomain = myxml.doc umentelement. selectnodes ("// domain ")
Rem -- the number of nodes in the list
Numofnodes = nodesurl. Length
Resourcelist = "<font face = verdana size = 2> latest ASP. NET Resources </font> <ul>"
For I = 0 to numofnodes-1
Resourcelist = resourcelist & "<li> <a href =" & nodesurl (I). Text & "> <font face = verdana size = 2> "&
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>
  
The same idea can be used in other programming languages.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.