ASP calls WebService

Source: Internet
Author: User
1. Soap Request Method
2. POST Request Method
3. showallnode function (displays attributes and data of nodes)
---------------------
I. SOAP request example
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"





string
string



to interact With WebService, You need to construct a SOAP request identical to 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 (soapr Equest)
'. In this way, XMLHTTP is used to successfully send a SOAP request that matches the soap sample.
'check whether the response is successful:
response. write XMLHTTP. status & "& nbsp;"
response. write XMLHTTP. statustext
set XMLHTTP = nothing
%>
If the call succeeds, the 200 OK message is displayed. If the call fails, the 500 internal server error occurs: keep connection: keep-alive.
after successful response, you can use the WebService response as follows:
soap response example
below 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' indicates 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

Ii. POST request example
HTTP POST
The following is an example of an http post request. The displayed placeholder must be replaced by the actual value.
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
Construct a POST request:
<%
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" 'Note
XMLHTTP. setRequestHeader "host", "192.100.100.81"
XMLHTTP. setRequestHeader "Content-Length", Len (soaprequest)

XMLHTTP. Send (soaprequest)
'In this way, the POST request with the http post sample is successfully sent using XMLHTTP.
'Check whether it is successful:
Response. Write XMLHTTP. Status & "& nbsp ;"
Response. Write XMLHTTP. statustext
Set XMLHTTP = nothing
%>
If it succeeds, the system displays 200 OK. If it fails, the system displays 500 internal server error: keep connection: keep-alive.
The WebService response can be used as follows:
HTTP POST
The following is an http post 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"?>
<String xmlns = "http://tempuri.org/"> string </string>

Display:
If xml http. Status = 200 then

Set xmldoc = server. Createobject ("MSXML. domdocument ")
Xmldoc. Load (XMLHTTP. responsexml)
Showallnode "string", xmldoc 'calls showallnode
Set xmldoc = nothing

Else

Response. Write XMLHTTP. Status & "& nbsp ;"
Response. Write XMLHTTP. statustext

End if

above is ASP sends SOAP request with XMLHTTP component, call WebService method, I recommend using the first method in ASP environment, if there is a better way please contact me mailto: lyq8442002@msn.com. if you use http get, there will be problems with Chinese characters, and the amount of data is not large. The method of using http post is like this. In fact, the above example is the POST method, but not the POST request. The software must be installed with the soap toolkit, and no subsequent version is available. --- Full text

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.