教程 > SOAP > SOAP 基本教程 阅读:44

SOAP Transport 传输

SOAP 不依赖于任何传输协议。 SOAP 可以通过 SMTP、FTP、IBM 的 MQSeries 或 Microsoft 消息队列 (MSMQ) 传输。

SOAP 规范仅包含有关 HTTP 的详细信息。 HTTP 仍然是最流行的 SOAP 传输协议。


通过 HTTP 的 SOAP

从逻辑上讲,SOAP 请求是通过 HTTP 请求发送的,并且 SOAP 响应在 HTTP 响应的内容中返回。虽然可以通过 HTTP GET 发送 SOAP 请求,但该规范仅包含有关 HTTP POST 的详细信息。

此外,HTTP 请求和响应都需要将其内容类型设置为 text/xml

SOAP 规范要求客户端必须提供 SOAPAction 标头,但 SOAPAction 标头的实际值取决于 SOAP 服务器实现。

例如,要访问由 XMethods 托管的 AltaVista BabelFish Translation 服务,您必须将以下内容指定为 SOAPAction 标头。

urn:xmethodsBabelFish#BabelFish

即使服务器不需要完整的 SOAPAction 标头,客户端也必须指定空字符串 ("") 或空值。例如 -

SOAPAction: ""
SOAPAction:

这是通过 HTTP 发送到 XMethods Babelfish 翻译服务的示例请求 -

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 标头。另请注意,BabelFish 方法需要两个 String 参数。翻译模式 en_fr 将英语翻译成法语。

这是 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 元素。

查看笔记

扫码一下
查看教程更方便