Soap 简明教程

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.