How ASP invokes WebService

Source: Internet
Author: User
Tags chr end http post soap version xmlns advantage
1. Soap Request method
2. Post Request method
3. Showallnode function (about node properties and data Display)

A SOAP Request Sample

The following is an example of a SOAP request. The displayed placeholder needs to 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= "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd= "Http://www.w3.org/2001/XMLSchema "Xmlns:soap=" http://schemas.xmlsoap.org/soap/envelope/"
<soap:Body>
<loginbyaccount xmlns= "http://tempuri.org/"
<username> string </username>
<password> string </password>
</LoginByAccount>
</soap:Body>
</soap:Envelope>

In order to interact with WebService, you need to construct a SOAP request that is identical to the one above:

<%
url = "Http://192.100.100.81/WebService1/UserSignOn.asmx"

soaprequest= "<?xml version=" &AMP;CHR (a) & "1.0" &AMP;CHR (a) & "encoding=" &AMP;CHR (a) & "Utf-8" &AMP;CHR ( & "? >" & _
"<soap:envelope xmlns:xsi=" &AMP;CHR & "Http://www.w3.org/2001/XMLSchema-instance" &AMP;CHR (+) & "" & _
"Xmlns:xsd=" &AMP;CHR & "Http://www.w3.org/2001/XMLSchema" &AMP;CHR (+) & "" & _
"Xmlns:soap=" &AMP;CHR & "http://schemas.xmlsoap.org/soap/envelope/" &AMP;CHR (+) & ">" & _
"<soap:Body>" & _

"<loginbyaccount xmlns=" &AMP;CHR & "http://tempuri.org/" &AMP;CHR (+) & ">" & _
"<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)
' This makes use of XMLHTTP to successfully send a SOAP request with the SOAP sample.
' Check for success:
Response.Write XMLHTTP. status& ""
Response.Write XMLHTTP. StatusText
Set xmlhttp = Nothing
%>
If success will show OK, unsuccessful will show 500 internal server error? Connection:keep-alive.
Once successful, you can take advantage of the WebService response, as follows:
SOAP response Sample
The following is an example of a SOAP response. The displayed placeholder needs to be replaced by the actual value.
http/1.1 OK
Content-type:text/xml; Charset=utf-8
Content-length:length

<?xml version= "1.0" encoding= "Utf-8"?
<soap:envelope xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd= "Http://www.w3.org/2001/XMLSchema "Xmlns:soap=" http://schemas.xmlsoap.org/soap/envelope/"
<soap:Body>
<loginbyaccountresponse xmlns= "http://tempuri.org/"
<LoginByAccountResult> string </LoginByAccountResult>
</LoginByAccountResponse>
</soap:Body>
</soap:Envelope>
This is an example of the SOAP response that corresponds to the sample SOAP request, and you can view the response after the request has been successfully sent:
If XMLHTTP. Status = 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
The correct request gives a full response, the request is incorrect (such as account number, password is not correct) the content of the response will be incomplete.
Remove the data from the response as follows:
If XMLHTTP. Status = Then

Set xmldoc = server. CreateObject ("MSXML"). DOMDocument ")
Xmldoc.load (Xmlhttp.responsexml)
Response.Write XmlDOC.documentElement.selectNodes ("//loginbyaccountresult") (0). Text ' Display data with Loginbyaccountresult nodes (encoded to decode)
Set xmldoc = Nothing

Else

Response.Write XMLHTTP. status& ""
Response.Write XMLHTTP. StatusText


End If

Displays the function of individual properties and data for a node:

Function Showallnode (Rootname,myxmldoc) ' Hope everyone is constantly finished Shanshan 2005-1-9 writed by 844
If Rootname <> "" Then

Set Nodeobj=myxmldoc.documentelement.selectsinglenode ("//&rootname&") ' current node pair image
Nodeattributelen=myxmldoc.documentelement.selectsinglenode ("//&rootname&"). Attributes.length ' current node attribute number

returnstring=returnstring& "<BR> node Name:" &rootname

If Nodeobj.text <> "" Then
Text of the returnstring=returnstring& <BR> node: ("&nodeobj.text&")
End If

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

If Nodeattributelen <> 0 Then
returnstring=returnstring& <BR> The number of properties is &nodeAttributelen&, respectively: "
End If

For I=0 to NodeAttributelen-1
returnstring=returnstring& "<li>" &nodeobj.attributes (i). name& ":" &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" has child nodes
Set Childnodeobj=nodeobj.childnodes
Childnodelen=nodeobj.childnodes.length
returnstring=returnstring& "<BR> <BR> has a" &childnodelen& "child node; The <BR> is: "
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

You can use this:
If XMLHTTP. Status = Then

Set xmldoc = server. CreateObject ("MSXML"). DOMDocument ")
Xmldoc.load (Xmlhttp.responsexml)
Showallnode "Loginbyaccountresponse", xmldoc ' call Showallnode
Set xmldoc = Nothing

Else

Response.Write XMLHTTP. status& ""
Response.Write XMLHTTP. StatusText

End If

Two Post Request Example
HTTP POST
The following is an example of an HTTP POST request. The displayed placeholder needs to 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
To 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" ' attention
Xmlhttp.setrequestheader "HOST", "192.100.100.81"
Xmlhttp.setrequestheader "Content-length", LEN (Soaprequest)

xmlHTTP. Send (Soaprequest)
' This will successfully send a POST request with the HTTP POST sample by using XMLHTTP.
' Check for success:
Response.Write XMLHTTP. status& ""
Response.Write XMLHTTP. StatusText
Set xmlhttp = Nothing
%>
If success will show OK, unsuccessful will show 500 internal server error? Connection:keep-alive.
Once successful, you can take advantage of the WebService response, as follows:
HTTP POST
The following is an example of an HTTP POST response. The displayed placeholder needs to be replaced by the actual value.
http/1.1 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>


Show:
If XMLHTTP. Status = Then

Set xmldoc = server. CreateObject ("MSXML"). DOMDocument ")
Xmldoc.load (Xmlhttp.responsexml)
Showallnode "string", xmldoc ' call Showallnode
Set xmldoc = Nothing

Else

Response.Write XMLHTTP. status& ""
Response.Write XMLHTTP. StatusText

End If


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.