Introduction to soap

Source: Internet
Author: User

Soap is a simple XML-based protocol that enables applicationsProgramExchange information over HTTP.

In our soap tutorial, you will learn what soap is and how it exchanges information between applications.

Start learning soap!

Content directory
Introduction to soap
The concept and function of soap.
Soap syntax
The soap construction module and the soap syntax rules.
SOAP envelope element
The envelope element of soap.
SOAP Header element
The Header element of soap.
Soap body Element
The soap Body element.
Soap Fault Element
Fault Element of soap.
Soap HTTP binding
Introduction to binding soap HTTP.
Soap instance
Describes a soap instance.

I. Introduction to soap
Soap is a simple XML-based protocol that allows applications to exchange information over HTTP.
Or, more simply, soap is the protocol used to access network services.

What is soap?
Soap refers to Simple Object Access Protocol
Soap is a communication protocol.
Soap is used for communication between applications.
Soap is a format used to send messages.
Soap is designed for communication over the Internet
Soap is independent from the platform
Soap is independent of Language
Soap Based on XML
Soap is simple and scalable
SOAP allows you to bypass the Firewall
Soap will be developed as W3C Standard

Why soap?
For application development, it is very important to enable Internet communication between applications.

Currently, applications use Remote Procedure Call (RPC) to communicate with objects such as DCOM and CORBA, but HTTP is not designed for this purpose.
RPC may cause compatibility and Security Issues. firewalls and proxy servers usually block such traffic.

Communication between applications through HTTP is better because HTTP is supported by all Internet browsers and servers. Soap is created to complete this task.

Soap provides a standard method for running on different operating systems and using different technologies andProgramming LanguageApplications can communicate with each other.
 
Microsoft and soap
Soap is a key element of Microsoft. NET architecture and is used for future Internet application development.
 

Ii. Soap syntax
Soap build Module
A soap message is a common XML document that contains the following elements:

1. The required envelope element identifies this XML document as a SOAP message.
2. optional Header elements, including header information
3. Required body elements, including all call and response information
4. An optional fault element that provides information about errors that occur when processing the message
All the above elements are declared in the default namespace for soap encapsulation:
Http://www.w3.org/2001/12/soap-envelope

And the default namespace for soap encoding and data types:
Http://www.w3.org/2001/12/soap-encoding

Syntax Rules
Here are some important syntax rules:

1. soap messages must be encoded in XML.
2. the SOAP message must use the SOAP envelope namespace.
3. the SOAP message must use the soap encoding namespace.
4. soap messages cannot contain DTD references
5. soap messages cannot contain XML processing instructions
 
Basic Structure of 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">

<Soap: Header>
...
...
</Soap: Header>

<Soap: Body>
...
...
<Soap: Fault>
...
...
</Soap: Fault>
</Soap: Body>

</Soap: envelope>

3. SOAP envelope element
The envelope element of the forced soap is the root element of the SOAP message.
SOAP envelope element
The required SOAP envelope element is the root element of the SOAP message. It defines an XML document as a SOAP message, that is, it identifies this XML document as a sqap message.

Xmlns: Soap namespace
A soap message must have an envelope element associated with the namespace "http://www.w3.org/2001/12/soap-envelope.
If different namespaces are used, an error occurs in the application and the message is discarded.
 
Encodingstyle attributes
The encodingstyle attribute of soap is used to define the data type used in the document.
This attribute can appear in any soap element and will be applied to the content of the element and all child elements of the element.
No default encoding method is available for soap messages.

Iv. SOAP header Elements
The optional SOAP Header element contains header information.
Optional SOAP header elements can contain application-specific information (such as authentication and payment) about soap messages ).
If the Header element is provided, it must be the first child element of the envelope element.

Note: The direct sub-elements of all header elements must be qualified namespaces.
<? 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 = "http://www.w3school.com.cn/transaction/" Soap: mustunderstand = "1"> 234 </M: Trans>
</Soap: Header>
...
...
</Soap: envelope>

The preceding example contains a header with a "trans" element. Its value is 234. The value of the "mustunderstand" attribute of this element is "1 ".
In the default namespace ("http://www.w3.org/2001/12/soap-envelope"), Soap defines three attributes.
These three attributes are: Actor, mustunderstand, and encodingstyle. These attributes defined in the SOAP header can define how the container processes soap messages.

Actor attributes
By passing through different endpoints along the message path, soap messages can be transmitted from a sender to a receiver.
Not all parts of a SOAP message are intended to be transmitted to the end endpoint of the SOAP message. However, another aspect may be intended to be transmitted to one or more endpoints on the message path.

The actor attribute of soap can be used to address the Header element to a specific endpoint.
Syntax:
Soap: Actor = "Uri"

Instance:
<? 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 = "http://www.w3school.com.cn/transaction/" Soap: Actor = "http://www.w3school.com.cn/appml/">
234
</M: Trans>
</Soap: Header>
...
...
</Soap: envelope>

Mustunderstand attributes
The mustunderstand attribute of soap can be used to identify whether a title item is mandatory or optional for the recipient to process it.
If you add "mustunderstand =" 1 "to a child element of the header, it indicates that the recipient processing this header must recognize this element. If the recipient cannot recognize this element, it must be invalid when processing this header.

Syntax:
Soap: mustunderstand = "0 | 1" instance
<? 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 = "http://www.w3school.com.cn/transaction/" Soap: mustunderstand = "1">
234
</M: Trans>
</Soap: Header>
...
...
</Soap: envelope>
 
5. Soap body Element
The forced soap Body element contains the actual SOAP message.
Soap body Element
The required soap Body element can contain the actual SOAP message to be sent to the Message endpoint.

The direct sub-element of the soap Body element can be a qualified namespace.
In the default namespace ("http://www.w3.org/2001/12/soap-envelope"), Soap defines an element inside the body element.
That is, the fault element of soap, used to indicate error 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">
<Soap: Body>
<M: getprice xmlns: M = "http://www.w3school.com.cn/prices">
<M: item> apples </M: item>
</M: getprice>
</Soap: Body>
</Soap: envelope>
 
The above example requests the price of apple.
Note that the above M: getprice and item elements are application-specific elements. They are not part of the soap standard.

A soap response should be similar to the following:

<? 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 = "http://www.w3school.com.cn/prices">
<M: price> 1.90 </M: price>
</M: getpriceresponse>
</Soap: Body>
</Soap: envelope>

Vi. Soap Fault Element
The optional soap fault element is used to retain the error and status information of the SOAP message.
Soap Fault Element
Error messages from soap messages are carried inside the fault element.

If a fault element is provided, it must be a child element of the Body element. In a SOAP message, the fault element can only appear once.

The Fault Element of soap is used for the sub-elements of the following columns:

sub-element description
Code
fault description
information about who caused the fault
persists application-specific error information related to the body element
soap fault codes
the faultcode value defined below must be used to describe the faultcode element when a fault occurs:

Error description
Invalid namespace of versionmismatch SOAP envelope element found
A direct sub-element of the mustunderstand Header element (with the mustunderstand attribute set to "1") cannot be understood.
The client message is incorrectly formed or contains incorrect information.
The server is faulty, so it cannot be processed.


VII. Soap HTTP binding
HTTP protocol
HTTP communicates over TCP/IP. The HTTP client uses TCP to connect to the HTTP server. After a connection is established, the client can send an HTTP request message to the server:

Post or item HTTP/1.1
HOST: 189.123.345.239
Content-Type: text/plain
Content-Length: 200
The server then processes the request and sends an HTTP Response to the client. This response contains the status code indicating the Request status:

200 OK
Content-Type: text/plain
Content-Length: 200
In the preceding example, the server returns a 200 status code. This is the standard success code of HTTP.

If the server cannot decode the request, it may return such information:

400 bad request
Content-Length: 0
Soap HTTP binding
 
A soap method is an HTTP request/response that complies with the soap encoding rules.

HTTP + xml = soap

The soap request may be an http post or http get request.

The http post request requires at least two HTTP headers: Content-Type and Content-Length.
Content-Type
The Content-Type header of the SOAP request and response defines the MIME type of the message and the character encoding of the XML Entity used for the request or response (optional ).

Syntax
Content-Type: mimetype; charset = character-Encoding
Example
Post or item HTTP/1.1
Content-Type: Application/soap + XML; charset = utf-8Content-Length
The Content-Length header of the SOAP request and response specifies the number of bytes of the request or response body.

Syntax
Content-Length: bytes
Example
Post or item HTTP/1.1
Content-Type: Application/soap + XML; charset = UTF-8
Content-Length: 250

8. One instance

Soap instance
A soap instance
In the following example, A getstockprice request is sent to the server. This request has a stockname parameter, and a price parameter is returned in the response.
The namespace of this function is defined in this address :"Http://www.example.org/stock"

SOAP request:
Post or instock HTTP/1.1
Host:Http://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>

Soap response:
HTTP/1.1 200 OK
Content-Type: Application/soap + XML; charset = UTF-8
Content-Length: NNN



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>

 

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.