Soap 简明教程
What is SOAP?
SOAP 是简单对象访问协议的缩写。它是一种基于 XML 的消息传递协议,用于在计算机之间交换信息。SOAP 是 XML 规范的应用程序。
SOAP is an acronym for Simple Object Access Protocol. It is an XML-based messaging protocol for exchanging information among computers. SOAP is an application of the XML specification.
Points to Note
-
SOAP is a communication protocol designed to communicate via Internet.
-
SOAP can extend HTTP for XML messaging.
-
SOAP provides data transport for Web services.
-
SOAP can exchange complete documents or call a remote procedure.
-
SOAP can be used for broadcasting a message.
-
SOAP is platform- and language-independent.
-
SOAP is the XML way of defining what information is sent and how.
-
SOAP enables client applications to easily connect to remote services and invoke remote methods.
虽然 SOAP 可用于各种消息传递系统,并且可以通过各种传输协议进行传递,但 SOAP 的最初重点是通过 HTTP 传输的远程过程调用。
Although SOAP can be used in a variety of messaging systems and can be delivered via a variety of transport protocols, the initial focus of SOAP is remote procedure calls transported via HTTP.
包括 CORBA、DCOM 和 Java RMI 在内的其他框架为 SOAP 提供了类似的功能,但 SOAP 消息完全用 XML 编写,因此独立于平台和语言。
Other frameworks including CORBA, DCOM, and Java RMI provide similar functionality to SOAP, but SOAP messages are written entirely in XML and are therefore uniquely platform- and language-independent.
SOAP - Message Structure
一条 SOAP 消息是一个普通的 XML 文档,包含以下元素:
A SOAP message is an ordinary XML document containing the following elements −
-
Envelope − Defines the start and the end of the message. It is a mandatory element.
-
Header − Contains any optional attributes of the message used in processing the message, either at an intermediary point or at the ultimate end-point. It is an optional element.
-
Body − Contains the XML data comprising the message being sent. It is a mandatory element.
-
Fault − An optional Fault element that provides information about errors that occur while processing the message.
所有这些元素都在 SOAP 封装的默认名称空间 http://www.w3.org/2001/12/soap-envelope 和 SOAP 编码和数据类型的默认名称空间 http://www.w3.org/2001/12/soap-encoding 中声明
All these elements are declared in the default namespace for the SOAP envelope − http://www.w3.org/2001/12/soap-envelope and the default namespace for SOAP encoding and data types is − http://www.w3.org/2001/12/soap-encoding
NOTE − 所有这些规范都可能更改。因此,请持续更新 W3 网站上提供的最新规范。
NOTE − All these specifications are subject to change. So keep updating yourself with the latest specifications available on the W3 website.
SOAP Message Structure
以下框图描述了 SOAP 消息的一般结构 -
The following block depicts the general structure of a SOAP message −
<?xml version = "1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
SOAP-ENV:encodingStyle = "http://www.w3.org/2001/12/soap-encoding">
<SOAP-ENV:Header>
...
...
</SOAP-ENV:Header>
<SOAP-ENV:Body>
...
...
<SOAP-ENV:Fault>
...
...
</SOAP-ENV:Fault>
...
</SOAP-ENV:Body>
</SOAP_ENV:Envelope>
SOAP - Envelope
SOAP 封装表明了消息的开始和结束,以便接收者知道何时收到一整条消息。SOAP 封装解决了这样一个问题,即知道你何时完成接收消息并准备好对其进行处理。因此,SOAP 封装基本上是一种打包机制。
The SOAP envelope indicates the start and the end of the message so that the receiver knows when an entire message has been received. The SOAP envelope solves the problem of knowing when you are done receiving a message and are ready to process it. The SOAP envelope is therefore basically a packaging mechanism.
Points to Note
-
Every SOAP message has a root Envelope element.
-
Envelope is a mandatory part of SOAP message.
-
Every Envelope element must contain exactly one Body element.
-
If an Envelope contains a Header element, it must contain no more than one, and it must appear as the first child of the Envelope, before the Body.
-
The envelope changes when SOAP versions change.
-
The SOAP envelope is specified using the ENV namespace prefix and the Envelope element.
-
The optional SOAP encoding is also specified using a namespace name and the optional encodingStyle element, which could also point to an encoding style other than the SOAP one.
-
A v1.1-compliant SOAP processor generates a fault upon receiving a message containing the v1.2 envelope namespace.
-
A v1.2-compliant SOAP processor generates a VersionMismatch fault if it receives a message that does not include the v1.2 envelope namespace.
v1.2-Compliant SOAP Message
以下是 v1.2 兼容的 SOAP 消息:
Given below is an example of v1.2-compliant SOAP message.
<?xml version = "1.0"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
SOAP-ENV:encodingStyle = " http://www.w3.org/2001/12/soap-encoding">
...
Message information goes here
...
</SOAP-ENV:Envelope>
SOAP with HTTP POST
以下示例说明在 HTTP POST 操作中使用 SOAP 消息,该操作会将消息发送给服务器。它显示了信封模式定义和编码规则模式定义的命名空间。HTTP 头中的 OrderEntry 引用是 tutorialspoint.com 网站上要调用的程序的名称。
The following example illustrates the use of a SOAP message within an HTTP POST operation, which sends the message to the server. It shows the namespaces for the envelope schema definition and for the schema definition of the encoding rules. The OrderEntry reference in the HTTP header is the name of the program to be invoked at the tutorialspoint.com website.
POST /OrderEntry HTTP/1.1
Host: www.tutorialspoint.com
Content-Type: application/soap; charset = "utf-8"
Content-Length: nnnn
<?xml version = "1.0"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
SOAP-ENV:encodingStyle = " http://www.w3.org/2001/12/soap-encoding">
...
Message information goes here
...
</SOAP-ENV:Envelope>
NOTE − HTTP 绑定指定服务的位置。
NOTE − The HTTP binding specifies the location of the service.
SOAP - Header
可选的 Header 元素为指定其他应用程序级别要求提供了一个灵活的框架。例如,Header 元素可用于为受密码保护的服务指定数字签名。同样,它可用于为按次付费的 SOAP 服务指定一个账号。
The optional Header element offers a flexible framework for specifying additional application-level requirements. For example, the Header element can be used to specify a digital signature for password-protected services. Likewise, it can be used to specify an account number for pay-per-use SOAP services.
Points to Note
-
It is an optional part of a SOAP message.
-
Header elements can occur multiple times.
-
Headers are intended to add new features and functionality.
-
The SOAP header contains header entries defined in a namespace.
-
The header is encoded as the first immediate child element of the SOAP envelope.
-
When multiple headers are defined, all immediate child elements of the SOAP header are interpreted as SOAP header blocks.
SOAP Header Attributes
SOAP 标头可以拥有以下两个特性:
A SOAP Header can have the following two attributes −
Actor attribute
SOAP 协议将消息路径定义为 SOAP 服务节点的列表。这些中间节点中的每一个都能执行一些处理,然后将消息转发给链中的下一个节点。通过设置 Actor 特性,客户端可以指定 SOAP 标头的接收者。
The SOAP protocol defines a message path as a list of SOAP service nodes. Each of these intermediate nodes can perform some processing and then forward the message to the next node in the chain. By setting the Actor attribute, the client can specify the recipient of the SOAP header.
MustUnderstand attribute
它表示 Header 元素是可选的还是强制的。如果设置为 true,则接收者必须理解并根据其定义的语义处理 Header 特性,或者返回一个故障。
It indicates whether a Header element is optional or mandatory. If set to true, the recipient must understand and process the Header attribute according to its defined semantics, or return a fault.
以下示例显示如何在 SOAP 消息中使用 Header。
The following example shows how to use a Header in a SOAP message.
<?xml version = "1.0"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV = " http://www.w3.org/2001/12/soap-envelope"
SOAP-ENV:encodingStyle = " http://www.w3.org/2001/12/soap-encoding">
<SOAP-ENV:Header>
<t:Transaction
xmlns:t = "http://www.tutorialspoint.com/transaction/"
SOAP-ENV:mustUnderstand = "true">5
</t:Transaction>
</SOAP-ENV:Header>
...
...
</SOAP-ENV:Envelope>
SOAP - Body
SOAP 正文是包含 SOAP 消息中交换的应用程序定义 XML 数据的强制元素。正文必须包含在信封内,并且必须遵循为该消息定义的任何标头。
The SOAP body is a mandatory element that contains the application-defined XML data being exchanged in the SOAP message. The body must be contained within the envelope and must follow any headers that might be defined for the message.
正文被定义为信封的一个子元素,正文的语义在关联的 SOAP 模式中定义。
The body is defined as a child element of the envelope, and the semantics for the body are defined in the associated SOAP schema.
正文包含专为消息的最终接收者准备的强制信息。例如 −
The body contains mandatory information intended for the ultimate receiver of the message. For example −
<?xml version = "1.0"?>
<SOAP-ENV:Envelope>
........
<SOAP-ENV:Body>
<m:GetQuotation xmlns:m = "http://www.tp.com/Quotation">
<m:Item>Computers</m:Item>
</m:GetQuotation>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
上面的示例请求报价计算机套装。请注意,上面的 m:GetQuotation 和 Item 元素是应用程序特定元素。它们不是 SOAP 标准的一部分。
The example above requests a quotation of computer sets. Note that the m:GetQuotation and the Item elements above are application-specific elements. They are not a part of the SOAP standard.
以下是上述查询的响应 −
Here is the response to the above query −
<?xml version = "1.0"?>
<SOAP-ENV:Envelope>
........
<SOAP-ENV:Body>
<m:GetQuotationResponse xmlns:m = "http://www.tp.com/Quotation">
<m:Quotation>This is Qutation</m:Quotation>
</m:GetQuotationResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
通常,应用程序还会定义一个架构来包含与请求和响应元素相关的语义。
Normally, the application also defines a schema to contain semantics associated with the request and response elements.
Quotation 服务可能使用在应用程序服务器中运行的 EJB 来实现;如果是这样,SOAP 处理器将负责将正文信息映射为 GetQuotationResponse 服务的 EJB 实现中和之外的参数。SOAP 处理器还可以将正文信息映射到 .NET 对象、CORBA 对象、COBOL 程序等。
The Quotation service might be implemented using an EJB running in an application server; if so, the SOAP processor would be responsible for mapping the body information as parameters into and out of the EJB implementation of the GetQuotationResponse service. The SOAP processor could also be mapping the body information to a .NET object, a CORBA object, a COBOL program, and so on.
SOAP - Fault
如果在处理过程中发生错误,则对 SOAP 消息的响应将是消息正文中的 SOAP 错误元素,并且该错误将返回给 SOAP 消息的发送者。
If an error occurs during processing, the response to a SOAP message is a SOAP fault element in the body of the message, and the fault is returned to the sender of the SOAP message.
SOAP 错误机制返回有关错误的具体信息,包括预定义代码、说明和生成错误的 SOAP 处理器的地址。
The SOAP fault mechanism returns specific information about the error, including a predefined code, a description, and the address of the SOAP processor that generated the fault.
Points to Note
-
A SOAP message can carry only one fault block.
-
Fault is an optional part of a SOAP message.
-
For HTTP binding, a successful response is linked to the 200 to 299 range of status codes.
-
SOAP Fault is linked to the 500 to 599 range of status codes.
Sub-elements of Fault
SOAP 错误具有以下子元素 −
The SOAP Fault has the following sub elements −
Sr.No |
Sub-element & Description |
1 |
<faultCode> It is a text code used to indicate a class of errors. See the next Table for a listing of predefined fault codes. |
2 |
<faultString> It is a text message explaining the error. |
3 |
<faultActor> It is a text string indicating who caused the fault. It is useful if the SOAP message travels through several nodes in the SOAP message path, and the client needs to know which node caused the error. A node that does not act as the ultimate destination must include a faultActor element. |
4 |
<detail> It is an element used to carry application-specific error messages. The detail element can contain child elements called detail entries. |
SOAP Fault Codes
描述错误时,必须在 faultcode 元素中使用下面定义的 faultCode 值。
The faultCode values defined below must be used in the faultcode element while describing faults.
Sr.No |
Error & Description |
1 |
SOAP-ENV:VersionMismatch Found an invalid namespace for the SOAP Envelope element. |
2 |
SOAP-ENV:MustUnderstand An immediate child element of the Header element, with the mustUnderstand attribute set to "1", was not understood. |
3 |
SOAP-ENV:Client The message was incorrectly formed or contained incorrect information. |
4 |
SOAP-ENV:Server There was a problem with the server, so the message could not proceed. |
SOAP Fault Example
以下代码是一个 Fault 示例。该客户端请求了一个名为 ValidateCreditCard 的方法,但是该服务并不支持此方法。这表示客户端请求错误,服务器返回以下 SOAP 响应 -
The following code is a sample Fault. The client has requested a method named ValidateCreditCard, but the service does not support such a method. This represents a client request error, and the server returns the following SOAP response −
<?xml version = '1.0' encoding = 'UTF-8'?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi = "http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd = "http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode xsi:type = "xsd:string">SOAP-ENV:Client</faultcode>
<faultstring xsi:type = "xsd:string">
Failed to locate method (ValidateCreditCard) in class (examplesCreditCard) at
/usr/local/ActivePerl-5.6/lib/site_perl/5.6.0/SOAP/Lite.pm line 1555.
</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOAP - Encoding
SOAP 包含用于编码数据类型的内置规则集。它使 SOAP 消息能够指示特定数据类型,如整数、浮点数、双精度数或数组。
SOAP includes a built-in set of rules for encoding data types. It enables the SOAP message to indicate specific data types, such as integers, floats, doubles, or arrays.
-
SOAP data types are divided into two broad categories − scalar types and compound types.
-
Scalar types contain exactly one value such as a last name, price, or product description.
-
Compound types contain multiple values such as a purchase order or a list of stock quotes.
-
Compound types are further subdivided into arrays and structs.
-
The encoding style for a SOAP message is set via the SOAP-ENV:encodingStyle attribute.
-
To use SOAP 1.1 encoding, use the value http://schemas.xmlsoap.org/soap/encoding/
-
To use SOAP 1.2 encoding, use the value http://www.w3.org/2001/12/soap-encoding
-
Latest SOAP specification adopts all the built-in types defined by XML Schema. Still, SOAP maintains its own convention for defining constructs not standardized by XML Schema, such as arrays and references.
Scalar Types
对于标量类型,SOAP 采用 XML Schema 规范指定的所有内置简单类型。这包括字符串、浮点、双精度浮点数和整数。
For scalar types, SOAP adopts all the built-in simple types specified by the XML Schema specification. This includes strings, floats, doubles, and integers.
下表列出了从 XML Schema Part 0 − Primer http://www.w3.org/TR/2000/WD-xmlschema-0-20000407/ 中摘录的主要简单类型
The following table lists the main simple types, excerpted from the XML Schema Part 0 − Primer http://www.w3.org/TR/2000/WD-xmlschema-0-20000407/
Simple Types Built-In to XML Schema |
Simple Type |
Example(s) |
string |
Confirm this is electric. |
boolean |
true, false, 1, 0. |
float |
-INF, -1E4, -0, 0, 12.78E-2, 12, INF, NaN. |
double |
-INF, -1E4, -0, 0, 12.78E-2, 12, INF, NaN. |
decimal |
-1.23, 0, 123.4, 1000.00. |
binary |
100010 |
integer |
-126789, -1, 0, 1, 126789. |
nonPositiveInteger |
-126789, -1, 0. |
negativeInteger |
-126789, -1. |
long |
-1, 12678967543233 |
int |
-1, 126789675 |
short |
-1, 12678 |
byte |
-1, 126 |
nonNegativeInteger |
0, 1, 126789 |
unsignedLong |
0, 12678967543233 |
unsignedInt |
0, 1267896754 |
unsignedShort |
0, 12678 |
unsignedByte |
0, 126 |
positiveInteger |
1, 126789. |
date |
1999-05-31, ---05. |
time |
13:20:00.000, 13:20:00.000-05:00 |
例如,下面是一个具有双精度数据类型的 SOAP 响应 −
For example, here is a SOAP response with a double data type −
<?xml version = '1.0' encoding = 'UTF-8'?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd = "http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:getPriceResponse
xmlns:ns1 = "urn:examples:priceservice"
SOAP-ENV:encodingStyle = "http://www.w3.org/2001/12/soap-encoding">
<return xsi:type = "xsd:double">54.99</return>
</ns1:getPriceResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Compound Types
SOAP 数组有一组非常具体的规则,要求您同时指定元素类型和数组大小。SOAP 还支持多维数组,但并非所有 SOAP 实现都支持多维功能。
SOAP arrays have a very specific set of rules, which require that you specify both the element type and array size. SOAP also supports multidimensional arrays, but not all SOAP implementations support multidimensional functionality.
要创建数组,您必须将其指定为数组的 xsi:type。数组还必须包含一个 arrayType 属性。此属性必需,用于指定所包含元素的数据类型和数组的维度。
To create an array, you must specify it as an xsi:type of array. The array must also include an arrayType attribute. This attribute is required to specify the data type for the contained elements and the dimension(s) of the array.
例如,以下属性指定一个包含 10 个双精度的值数组 −
For example, the following attribute specifies an array of 10 double values −
arrayType = "xsd:double[10]"
相比之下,以下属性指定一个二维字符串数组 −
In contrast, the following attribute specifies a two-dimensional array of strings −
arrayType = "xsd:string[5,5]"
下面是一个具有双精度值数组的 SOAP 响应示例 −
Here is a sample SOAP response with an array of double values −
<?xml version = '1.0' encoding = 'UTF-8'?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd = "http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:getPriceListResponse
xmlns:ns1 = "urn:examples:pricelistservice"
SOAP-ENV:encodingStyle = "http://www.w3.org/2001/12/soap-encoding">
<return xmlns:ns2 = "http://www.w3.org/2001/09/soap-encoding"
xsi:type = "ns2:Array" ns2:arrayType = "xsd:double[2]">
<item xsi:type = "xsd:double">54.99</item>
<item xsi:type = "xsd:double">19.99</item>
</return>
</ns1:getPriceListResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
结构包含多个值,但每个元素均使用唯一的访问器元素指定。例如,考虑产品目录中的一个项目。在此情况下,结构可能包含产品 SKU、产品名称、描述和价格。以下是此类结构在 SOAP 消息中表示的方式 −
Structs contain multiple values, but each element is specified with a unique accessor element. For example, consider an item within a product catalog. In this case, the struct might contain a product SKU, product name, description, and price. Here is how such a struct would be represented in a SOAP message −
<?xml version = '1.0' encoding = 'UTF-8'?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd = "http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:getProductResponse
xmlns:ns1 = "urn:examples:productservice"
SOAP-ENV:encodingStyle = "http://www.w3.org/2001/12/soap-encoding">
<return xmlns:ns2 = "urn:examples" xsi:type = "ns2:product">
<name xsi:type = "xsd:string">Red Hat Linux</name>
<price xsi:type = "xsd:double">54.99</price>
<description xsi:type = "xsd:string">
Red Hat Linux Operating System
</description>
<SKU xsi:type = "xsd:string">A358185</SKU>
</return>
</ns1:getProductResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
NOTE − 编写 SOAP 代码时,请注意正确的缩进。结构中的每个元素都通过唯一访问器名称指定。例如,上面的消息包括四个访问器元素 - 名称、价格、描述和 SKU。每个元素可以有自己的数据类型。例如,名称指定为字符串,而价格指定为双精度浮点数。
NOTE − Please take care of proper indentation while you write your SOAP code. Each element in a struct is specified with a unique accessor name. For example, the message above includes four accessor elements − name, price, description, and SKU. Each element can have its own data type. For example, name is specified as a string, whereas price is specified as double.
SOAP - Transport
SOAP 并不绑定到任何传输协议。SOAP 可以通过 SMTP、FTP、IBM 的 MQSeries 或 Microsoft Message Queuing (MSMQ) 运输。
SOAP is not tied to any transport protocol. SOAP can be transported via SMTP, FTP, IBM’s MQSeries, or Microsoft Message Queuing (MSMQ).
SOAP 规范仅包括对 HTTP 的详细信息。HTTP 仍然是最流行的 SOAP 传输协议。
SOAP specification includes details on HTTP only. HTTP remains the most popular SOAP transport protocol.
SOAP via HTTP
从逻辑上讲,SOAP 请求通过 HTTP 请求发送,SOAP 响应在 HTTP 响应内容中返回。尽管可以将 SOAP 请求通过 HTTP GET 发送,该规范中只包含了有关 HTTP POST 的详细信息。
Quite logically, SOAP requests are sent via an HTTP request and SOAP responses are returned within the content of the HTTP response. While SOAP requests can be sent via an HTTP GET, the specification includes details on HTTP POST only.
此外,需要将 HTTP 请求和响应的内容类型都设置为 text/xml。
Additionally, both HTTP requests and responses are required to set their content type to text/xml.
SOAP 规范要求客户端必须提供一个 SOAPAction header,但 SOAPAction header 的实际值取决于 SOAP 服务器的实现。
The SOAP specification mandates that the client must provide a SOAPAction header, but the actual value of the SOAPAction header is dependent on the SOAP server implementation.
例如,若要访问由 XMethods 托管的 AltaVista BabelFish 翻译服务,必须指定以下内容作为 SOAPAction header。
For example, to access the AltaVista BabelFish Translation service, hosted by XMethods, you must specify the following as a SOAPAction header.
urn:xmethodsBabelFish#BabelFish
即使服务器不需要完整的 SOAPAction header,客户端也必须指定一个空字符串(“”)或一个 null 值。例如 -
Even if the server does not require a full SOAPAction header, the client must specify an empty string ("") or a null value. For example −
SOAPAction: ""
SOAPAction:
以下是一个通过 HTTP 发送给 XMethods BabelFish 翻译服务的请求示例 -
Here is a sample request sent via HTTP to the XMethods Babelfish Translation service −
POST /perl/soaplite.cgi HTTP/1.0
Host: services.xmethods.com
Content-Type: text/xml; charset = utf-8
Content-Length: 538
SOAPAction: "urn:xmethodsBabelFish#BabelFish"
<?xml version = '1.0' encoding = 'UTF-8'?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi = "http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd = "http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<ns1:BabelFish
xmlns:ns1 = "urn:xmethodsBabelFish"
SOAP-ENV:encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/">
<translationmode xsi:type = "xsd:string">en_fr</translationmode>
<sourcedata xsi:type = "xsd:string">Hello, world!</sourcedata>
</ns1:BabelFish>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
注意内容类型和 SOAPAction header。另外,注意 BabelFish 方法需要两个 String 参数。en_fr 翻译模式将英语翻译成法语。
Note the content type and the SOAPAction header. Also note that the BabelFish method requires two String parameters. The translation mode en_fr translates from English to French.
以下是来自 XMethods 的响应 -
Here is the response from XMethods −
HTTP/1.1 200 OK
Date: Sat, 09 Jun 2001 15:01:55 GMT
Server: Apache/1.3.14 (Unix) tomcat/1.0 PHP/4.0.1pl2
SOAPServer: SOAP::Lite/Perl/0.50
Cache-Control: s-maxage = 60, proxy-revalidate
Content-Length: 539
Content-Type: text/xml
<?xml version = "1.0" encoding = "UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENC = "http://schemas.xmlsoap.org/soap/encoding/"
SOAP-ENV:encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi = "http://www.w3.org/1999/XMLSchema-instance"
xmlns:SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd = "http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<namesp1:BabelFishResponse xmlns:namesp1 = "urn:xmethodsBabelFish">
<return xsi:type = "xsd:string">Bonjour, monde!</return>
</namesp1:BabelFishResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
通过 HTTP 发送的 SOAP 响应需要遵循相同的 HTTP 状态代码。例如,状态代码 200 OK 表示成功响应。状态代码 500 Internal Server Error 表示服务器错误,并且 SOAP 响应中包含 Fault 元素。
SOAP responses delivered via HTTP are required to follow the same HTTP status codes. For example, a status code of 200 OK indicates a successful response. A status code of 500 Internal Server Error indicates that there is a server error and that the SOAP response includes a Fault element.
SOAP - Examples
在下面的示例中,一个 GetQuotation 请求通过 HTTP 发送到 SOAP 服务器。该请求有一个 QuotationName 参数,并且一个 Quotation 将在响应中返回。
In the example below, a GetQuotation request is sent to a SOAP Server over HTTP. The request has a QuotationName parameter, and a Quotation will be returned in the response.
函数的命名空间在 http://www.xyz.org/quotation address 中定义。
The namespace for the function is defined in http://www.xyz.org/quotation address.
以下是 SOAP 请求 -
Here is the SOAP request −
POST /Quotation HTTP/1.0
Host: www.xyz.org
Content-Type: text/xml; charset = utf-8
Content-Length: nnn
<?xml version = "1.0"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
SOAP-ENV:encodingStyle = "http://www.w3.org/2001/12/soap-encoding">
<SOAP-ENV:Body xmlns:m = "http://www.xyz.org/quotations">
<m:GetQuotation>
<m:QuotationsName>MiscroSoft</m:QuotationsName>
</m:GetQuotation>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
对应的 SOAP 响应如下 −
A corresponding SOAP response looks like −
HTTP/1.0 200 OK
Content-Type: text/xml; charset = utf-8
Content-Length: nnn
<?xml version = "1.0"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
SOAP-ENV:encodingStyle = "http://www.w3.org/2001/12/soap-encoding">
<SOAP-ENV:Body xmlns:m = "http://www.xyz.org/quotation">
<m:GetQuotationResponse>
<m:Quotation>Here is the quotation</m:Quotation>
</m:GetQuotationResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOAP - Standards
SOAP 1.1 最初于 2000 年 5 月提交给 W3C。官方提交者包括大型公司,如 Microsoft、IBM 和 Ariba,以及小型公司,如 UserLand Software 和 DevelopMentor。
SOAP 1.1 was originally submitted to the W3C in May 2000. Official submitters included large companies such as Microsoft, IBM, and Ariba, and smaller companies such as UserLand Software and DevelopMentor.
2001 年 7 月,XML 协议工作组发布了 SOAP 1.2 的“工作草案”。在 W3C 中,此文档正式的工作进程,这意味着在最终确定之前,该文档可能会多次更新。
In July 2001, the XML Protocol Working Group released a "working draft" of SOAP 1.2. Within the W3C, this document is officially a work in progress, meaning that the document is likely to be updated many times before it is finalized.
SOAP 1.1 版本可以在 http://www.w3.org/TR/SOAP/ 在线获得
SOAP Version 1.1 is available online at http://www.w3.org/TR/SOAP/
SOAP 1.2 版本的工作草案可以在 http://www.w3.org/TR/soap12/ 获得
The working draft of SOAP Version 1.2 is available at http://www.w3.org/TR/soap12/
请注意,W3C 还主持提交“带有附件的 SOAP 消息”,它与核心 SOAP 规范分离开来。此规范使 SOAP 消息能够包括图像和声音文件等二进制附件。如需了解详细信息,请参见 http://www.w3.org/TR/SOAP-attachments 处的 W3C 注意事项。
Note that the W3C also hosts a submission for "SOAP Messages with Attachments", which separates from the core SOAP specification. This specification enables SOAP messages to include binary attachments such as images and sound files. For full details, see the W3C Note at http://www.w3.org/TR/SOAP-attachments.