SOAP - 傳輸



SOAP 不繫結至任何傳輸協議。SOAP 可透過 SMTP、FTP、IBM 的 MQSeries 或 Microsoft Message Queuing (MSMQ) 傳輸。

SOAP 規範僅包含 HTTP 的詳情。HTTP 仍然是最流行的 SOAP 傳輸協議。

透過 HTTP 的 SOAP

相當具有邏輯性,SOAP 請求透過 HTTP 請求傳送,SOAP 響應在 HTTP 響應內容中返回。雖然可以 SOAP 請求透過 HTTP GET 傳送,但此規範僅包含 HTTP POST 的詳情。

此外,HTTP 請求和響應都需要將其內容型別設定為 text/xml。

SOAP 規範要求客戶端必須提供一個SOAPAction 頭部,但 SOAPAction 頭部的實際值取決於 SOAP 伺服器的實現。

例如,要訪問 XMethods 託管的 AltaVista BabelFish 翻譯服務,您必須將以下項指定為 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 元素。

廣告