SOAP Tutorial

Refer SOAP Tutorial

1 Introduction

SOAP is interpreted in Chinese as: Simple Object Access Protocol.
SOAP is a simple XML-based protocol that enables applications to exchange information over HTTP.
SOAP is a simple XML-based protocol that enables applications to exchange information over HTTP. Or to put it more simply: SOAP is a protocol for accessing web services.

Why use SOAP?
It is important for application development to enable Internet communication between programs. Today's applications communicate between objects such as > DCOM and CORBA by using Remote Procedure Calls (RPC), but HTTP was not designed for this. RPC> creates compatibility as well as security issues; firewalls and proxy servers often block such traffic. Communicating between applications via HTTP is a better approach because HTTP> is supported by all Internet browsers and servers. SOAP was created to accomplish this task. SOAP> provides a standard method that enables applications running on different operating systems and using different technologies and programming languages ​​to communicate with each other.

2. SOAP building blocks

A SOAP message is an ordinary XML document containing the following elements:

  • A required Envelope element that identifies this XML document as a SOAP message
  • Optional Header element, containing header information
  • Required Body element, contains all call and response information
  • An optional Fault element providing information about an error that occurred while processing this message

All of the above elements are declared in the default namespace for SOAP envelopes:
http://www.w3.org/2001/12/soap-envelope
and for SOAP encodings and data types:
http:// www.w3.org/2001/12/soap-encoding

2.1. Grammatical rules

Here are some important grammar rules:

  • SOAP messages must be encoded in XML
  • SOAP messages must use the SOAP Envelope namespace
  • SOAP messages must use the SOAP Encoding namespace
  • SOAP messages cannot contain DTD references
  • SOAP messages cannot contain XML processing instructions

2.2. Basic structure of SOAP message

 <?xml version="1.0"?>
 <soap:Envelope
 xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
 soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

 <soap:Header>
 ...
 </soap:Header>

 <soap:Body>
 ...
   <soap:Fault>
   ...
   </soap:Fault>
 </soap:Body>

 </soap:Envelope> 

2.3. Envelope elements

The root element of a SOAP message. It defines XML documents as SOAP messages.

 <?xml version="1.0"?>
 <soap:Envelope
 xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
 soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
   ...
   Message information goes here
   ...
 </soap:Envelope> 

1. xmlns:soap namespace

A SOAP message must have an Envelope element associated with the namespace "http://www.w3.org/2001/12/soap-envelope".
If a different namespace is used, the application will error out and discard this message.

2. encodingStyle attribute

The encodingStyle attribute of SOAP is used to define the data type used in the document. This attribute can appear on any SOAP element and will be applied to the element's content and all child elements of the element.
SOAP messages have no default encoding.

2.4. Header element

SOAP Header is an XML language tag that can contain application-specific information about a SOAP message (such as authentication, payment, etc.).
If a Header element is provided, it must be the first child element of the Envelope element .
Note: All immediate child elements of the Header element must be namespace-qualified.

<?xml version="1.0"?>        
<soap:Envelope        
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"        
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
                
<soap:Header>        
  <m:Trans xmlns:m="//www.w3cschool.cn/transaction/"        
  soap:mustUnderstand="1">234        
  </m:Trans>        
</soap:Header>        
...        
...        
</soap:Envelope>

The example above contains a header with a "Trans" element with a value of 234 and a "mustUnderstand" attribute with a value of "1".
SOAP defines three properties in the default namespace ("http://www.w3.org/2001/12/soap-envelope").
The three properties are: actor, mustUnderstand, and encodingStyle. These attributes defined in the SOAP header define how the container handles the SOAP message.

1. mustUnderstand attribute

The mustUnderstand attribute of SOAP can be used to identify whether a header item is mandatory or optional for the recipient to process it .
If you add "mustUnderstand="1" to a sub-element of the Header element, it indicates that the recipient processing this header must recognize this element. If the recipient cannot recognize this element, when processing this header must fail.

grammar:soap:mustUnderstand="0|1"

2. actor attribute

SOAP messages travel from a sender to a receiver by passing through different endpoints along the message path. Not all parts of a SOAP message are intended to be delivered to the final endpoint of the SOAP message, but, on the other hand, may be intended to be delivered to one or more endpoints along the message's path.

SOAP's actor attribute can be used to address the Header element to a specific endpoint.

<?xml version="1.0"?>        
<soap:Envelope        
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"        
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">        
        
<soap:Header>        
  <m:Trans xmlns:m="//www.w3cschool.cn/transaction/"        
  soap:actor="//www.w3cschool.cn/appml/">234        
  </m:Trans>        
</soap:Header>        
...        
...        
</soap:Envelope>

3. encodingStyle attribute

The encodingStyle attribute of SOAP is used to define the data type used in the document. This attribute can appear on any SOAP element and will be applied to the element's content and all child elements of the element.

SOAP messages have no default encoding.

2.5. Body element

The required SOAP Body element can contain the actual SOAP message intended to be delivered to the message's ultimate endpoint.
Direct child elements of the SOAP Body element can be namespace-qualified.

ask

<?xml version="1.0"?>
 <soap:Envelope
 xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
 soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

 <soap:Body>
   <m:GetPrice xmlns:m="//www.w3cschool.cn/prices">
     <m:Item>Apples</m:Item>
   </m:GetPrice>
 </soap:Body>

 </soap:Envelope>

The above example requests the price of apples. Note that the m:GetPrice and Item elements above are application-specific elements. They are not part of the SOAP standard.

And a SOAP response should look like this:

 <?xml version="1.0"?>
 <soap:Envelope
 xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
 soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

 <soap:Body>
   <m:GetPriceResponse xmlns:m="//www.w3cschool.cn/prices">
     <m:Price>1.90</m:Price>
   </m:GetPriceResponse>
 </soap:Body>

 </soap:Envelope> 

2.6, Fault element

Used to persist error and status information for SOAP messages.
If a Fault element is provided, it must be a child of the Body element. In a SOAP message, the Fault element can only appear once.
insert image description here

2.7、HTTP Binding

SOAP HTTP Binding
SOAP methods refer to HTTP requests/responses that follow the SOAP encoding rules.

HTTP + XML = SOAP
SOAP requests may be HTTP POST or HTTP GET requests.

An HTTP POST request specifies at least two HTTP headers: Content-Type and Content-Length.

1、Content-Type

The Content-Type header of SOAP's requests and responses defines the MIME type of the message, and optionally, the character encoding for the XML body of the request or response.

 POST /item HTTP/1.1
 Content-Type: application/soap+xml; charset=utf-8

2、Content-Length

The Content-Length header of SOAP requests and responses specifies the number of bytes in the request or response body.

 POST /item HTTP/1.1
 Content-Type: application/soap+xml; charset=utf-8
 Content-Length: 250 

3. Example

A SOAP instance
In the example below, a GetStockPrice request is sent to the server. This request has a StockName parameter, and in the response a Price parameter is returned. The namespace for this feature is defined at this address: "http://www.example.org/stock"

ask

 POST /InStock HTTP/1.1
 Host: www.example.org
 Content-Type: application/soap+xml; charset=utf-8
 Content-Length: nnn

 <?xml version="1.0"?>
 <soap:Envelope
 xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
 soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

 <soap:Body xmlns:m="http://www.example.org/stock">
   <m:GetStockPrice>
     <m:StockName>IBM</m:StockName>
   </m:GetStockPrice>
 </soap:Body>

 </soap:Envelope>

response

 HTTP/1.1 200 OK
 Content-Type: application/soap+xml; charset=utf-8
 Content-Length: nnn

 <?xml version="1.0"?>
 <soap:Envelope
 xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
 soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

 <soap:Body xmlns:m="http://www.example.org/stock">
   <m:GetStockPriceResponse>
     <m:Price>34.5</m:Price>
   </m:GetStockPriceResponse>
 </soap:Body>

 </soap:Envelope> 

Guess you like

Origin blog.csdn.net/weixin_41544662/article/details/131374200