XML SOAP Application Introduction

zhaozj2021-02-16  42

If you want to know what SOAP is, you have to create your own SOAP standard object yourself. This article can help you start. ==============================================

Introduction SOAP - Simple Object Access Protocol - is a hotspot developed by the current XML. It is the main role of Microsoft's new generation of Visual Studio, is the foundation of ".NET" strategy. If you want to use VB 6, you can check with Microsoft's SOAP toolkit (VB). But if you want to know what SOAP is, you have to create your own SOAP standard object yourself. This article can help you start.

In this article, we create a simple SOAP server and a client. The service is written with ASP, named soap.asp. This file should be stored in the root directory of the personal web server, such as: / inetpub / wwwroot. This server will accept and process the SOAP request proposed by the client. The client is a simple VB executable that is launched by Sub Main ().

The steps are known that SOAP is a "call-response" mechanism, running according to customer / service. The client (application) issues a function call request and transmitting parameters to the server (a web server located on the Internet); the server is returned. The content and data of the call and the response are transmitted in the XML file format. Therefore, to establish a simple SOAP application, create a client and one server, a call-response system.

Here is a simple example: We build a server to calculate the tax of the sales transaction. Follow the traditional VB terminology, establish a function, defined as follows: public function getSalesTax (Byval Psalestotal As Double) As double getsalestax = psalestotal * 0.04 end function

A rough function, but can be used as an example (this example can only be used in a place in the tax rate of 4%). This function defines a function name (GetSalesTax), a parameter (PSAleStotal - Sales) and a return value (function return value). According to the object-oriented principle, it can be considered to be a "IN" parameter, and the GetSalesTax return value is an "OUT" parameter. Therefore, our SOAP server is to listen to the "IN" parameter (sales amount) (sales amount) that the customer issued to call GetSalesTax, and then returns a response with the "OUT" parameter to return the required tax.

The client is the client program for establishing a call service with VB: dblsalestax = getSalesTax (100) gets the return value $ 4. If the getSalesTax function is an external object, for example on the MTS server, you want to call the DLL module on the server: Dim objtax as new cTaxcalc dblsalentAx = ObjTax.getsalesTax (100)

In the SOAP system, the remote call is slightly different, and the call is transmitted to the server via an XML file. XML files have called function names and corresponding parameters: 100 To ensure that the server can identify and interpret customer requests, call instructions are packaged to a large file called SOAP envelope in. This envelope uses the universal namespace of the SOAP package standard: 100

Finally, join the namespace called the function call, play a function declaration: < SOAP: BODY> 100

Now, the customer request file is ready to be sent to the server. The send request is simple, you can use the HTTP POST method as the browser form. The browser can send complex forms to the server, .NET can send VB code to the server, but I use XMLHTTP (IE 5 or later).

Suppose Strenvelope contains requests in XML file format, the send format is as follows: Dim objhttp as new msxml.xmlhttprequest Dim Strenvelope As String

'Setting to the local server objhttp.open "post", "http://localhost/soap/soap.asp"

'Set standard soap / XML file header Objhttp.seueStheader "Content-Type", "Text / XML"

'Setting the call function request objhtp.setRequestHeader "soapmethodname", _ "urn: myserver / soap: Taxcalc # getsalestax"

'Call SOAP Objhttp.send Strenvelope

'Get the return value strreturn = objhttp.responsebody

At this point, the client completes the process of sending a request to the server. Now return to the server, see how the server listens to the customer request and responds. The server server is able to receive the HTTP request sent by the customer, and respond when the local server (http://localhost/soap.asp) receives the customer request. Therefore, the server should be able to resolve the request of the XML format (SOAP package) issued by the client, take out the modes of the call.

The server file is soap.asp, which receives the approach of the customer request: set objReq = server.createObject ("Microsoft.xmLDom") Objreq.Load Request

Then remove the parameters from the packaged XML file with the XSL style: strQuery = "soap: envelope / soap: body / m: getsalestax / salestotal" VarsaStotal = ObjReq.selectsinglenode (strquery) .text

Calculate tax according to parameters: VarsaStax = VARSAASTOAL * 0.04

Before returning the result to the customer, press SOAP standard to format the package. The process is similar to the client, just convert the "in" parameter to "OUT" parameters, and responds the function tag name: 4

You can use a string to construct this response file or create a DOM object and add a node. After the file is returned to the customer, the customer can obtain the decoded results: Dim objReturn As New MSXML.DomDocument objReturn.LoadXML strReturn strQuery = _ "SOAP: Envelope / SOAP: Body / m: GetSalesTaxResponse / SalesTax" dblTax = objReturn.SelectSingleNode (strQuery ) .Text

This completes a simple SOAP service application. Although Visual Studio 7 masks the inner SOAP protocol, I hope this paper helps understand the operation of SOAP.

The following client code for VB: VB Client Code Sub Main () Dim objHTTP As New MSXML.XMLHTTPRequest Dim strEnvelope As String Dim strReturn As String Dim objReturn As New MSXML.DOMDocument Dim dblTax As Double Dim strQuery As String 'create SOAP envelopes strEnvelope = _ " & _ " " & _ " "& _" " & _ " 100 " & _ "" & _ " "& _" "Sets to the local server objhttp.open" post "," http://localhost/soap.asp ", false 'setting standard SOAP / XML format objhttp.setRequestHeader "content-type", "text / xml" set call function head objhttp.setRequestHeader "soapmethodname", _ "urn: myserver / soap: Taxcalculator # getsalestax" SOAP Call objHTTP.send strEnvelope 'return envelope extraction strReturn = objHTTP.responseText' is loaded into the DOM objReturn.loadXML strReturn 'query returns the value strQuery = _ "SOAP: Envelope / SOAP: Body / m: GetSalesTaxResponse / SalesTax" dblTax = objReturn.selectSingleNode (strQuery ) .Text debug.print DBLTAX END SUB The following is the server ASP code (file name soap.asp, stored in the root of the local server): <

% Set Objreq = Server.createObject ("Microsoft.xmLDom") 'loading request to XML DOM objReq.Load Request

'Inquiry strquery = "SOAP: envelope / soap: body / m: getsalestax / salestotal" VarsaStotal = ObjReq.selectsinglenode (strquery) .text

'Calculate VarsaStax = VARSAASTOAL * 0.04

'Preparing to return to envelope strtmp = _ " "& _" "& _ "& _" "& _" "& varsalestax &" " " "& _" "& _" "

'Copy result file Response.write strtmp%>

转载请注明原文地址:https://www.9cbs.com/read-27864.html

New Post(0)