You do not need to use. net or private components to access webservice through asp.

Source: Internet
Author: User
From: http://searchwebservices.techtarget.com.cn/tips/347/2136347_1.shtml
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.
######################################## #####

The method below is good

The following is a soap request example. The displayed placeholder must be replaced by the actual value.
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 = "The http://www.w3.org/2001/xmlSchema-instance" xmlns: xsd = "The http://www.w3.org/2001/xmlSchema" xmlns: soap = "The http://schemas.xmlsoap.org/soap/envelope/">
<Soap: Body>
<LoginByAccount xmlns = "http://tempuri.org/">
<Username> string </username>
<Password> string </password>
</LoginByAccount>
</Soap: Body>
</Soap: Envelope>
To interact With WEBSERVICE, You need to construct a soap request that is exactly the same as the preceding one:
<%
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" 'must be the same as the WEBSERVICE namespace, otherwise the service will reject
Xmlhttp. Send (soapRequest)
'Then, the soap request that matches the soap example is successfully sent using xmlHTTP.
'Check whether it is successful:
Response. Write xmlhttp. Status & "& nbsp ;"
Response. Write xmlhttp. StatusText
Set xmlhttp = Nothing
%>
If yes, the system displays 200 OK. If no, the system displays 500 internal server error? Connection: keep-alive.
The WEBSERVICE response can be used as follows:
Soap response example
The following is a soap response example. The displayed placeholder must be replaced by the actual value.
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 = "The http://www.w3.org/2001/xmlSchema-instance" xmlns: xsd = "The http://www.w3.org/2001/xmlSchema" xmlns: soap = "The http://schemas.xmlsoap.org/soap/envelope/">
<Soap: Body>
<LoginByAccountResponse xmlns = "http://tempuri.org/">
<LoginByAccountResult> string </LoginByAccountResult>
</LoginByAccountResponse>
</Soap: Body>
</Soap: Envelope>
This is the soap response example corresponding to the soap request example. After the request is successfully sent, you can view the response:
If xml http. 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
If the request is correct, a complete response is provided. If the request is incorrect (such as the account or password is incorrect), the response content is incomplete.
Retrieve the data in the response as follows:
If xml http. Status = 200 Then

Set xmlDOC = server. CreateObject ("MSxml. DOMDocument ")
XmlDOC. load (xmlhttp. responsexml)
Response. Write xmlDOC.doc umentElement. selectNodes ("// LoginByAccountResult") (0). text' displays data whose node is LoginByAccountResult (decoded if encoding is available)
Set xmlDOC = nothing

Else

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

End if

FUNCTION that displays the attributes and data of a node:

Function showallnode (rootname, myxmlDOC )''
If rootname <> "" then

Set nodeobj1_myxmldoc.doc umentElement. selectSingleNode ("//" & rootname & ")'' current node Image
Nodeattributelenode myxmldoc.doc umentElement. selectSingleNode ("//" & rootname & ""). attributes. length ''current node attribute count

Returnstring = returnstring & "<BR> node name:" & rootname

If nodeobj. text <> "" then
Returnstring = returnstring & "<BR> node text: (" & nodeobj. text &")"
End if

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

If nodeAttributelen <> 0 then
Returnstring = returnstring & "<BR> the number of attributes is & nbsp;" & nodeAttributelen & ", which are :"
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'' whether a subnode exists
Set childnodeobj = nodeobj. childNodes
Childnodelen = nodeobj. childNodes. Length
Returnstring = returnstring & "<BR> there are" & childnodelen & "subnodes. <BR> they are :"
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
It can be used as follows:
If xml http. Status = 200 Then

Set xmlDOC = server. CreateObject ("MSxml. DOMDocument ")
XmlDOC. load (xmlhttp. responsexml)
Showallnode "LoginByAccountResponse", xmlDOC 'calls SHOWALLNODE
Set xmlDOC = nothing

Else

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

End if

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.