问题

我对SOAP消息和WSDL如何组合在一起感到困惑?我已经开始研究SOAP消息,例如:

POST /InStock HTTP/1.1
Host: 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消息都是WSDL的吗? SOAP是一种接受自己的"SOAP消息"还是"WSDL"的协议?如果它们不同,那么我什么时候应该使用SOAP消息?何时应该使用WSDL?

对此进行一些澄清将是非常棒的。


#1 热门回答(109 赞)

每个请求都会发送一个SOAP文档。假设我们是一家书店,并且我们查询了一台远程服务器,以了解特定书籍的当前价格。假设我们需要将Book的标题,页数和ISBN号传递给服务器。

每当我们想知道价格时,我们都会发送一个唯一的SOAP消息。它看起来像这样;

<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <m:GetBookPrice xmlns:m="http://namespaces.my-example-book-info.com">
      <ISBN>978-0451524935</ISBN>
      <Title>1984</Title>
      <NumPages>328</NumPages>
    </m:GetBookPrice>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我们希望得到一条SOAP响应消息;

<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <m:GetBookPriceResponse xmlns:m="http://namespaces.my-example-book-info.com">
      <CurrentPrice>8.99</CurrentPrice>
      <Currency>USD</Currency>
    </m:GetBookPriceResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

然后,WSDL描述了当服务器接收到该消息时如何处理/处理该消息。在我们的例子中,它描述了Title,NumPages和ISBN的类型,我们是否应该期望GetBookPrice消息的响应以及响应应该是什么样的。

类型看起来像这样;

<wsdl:types>

  <!-- all type declarations are in a chunk of xsd -->
  <xsd:schema targetNamespace="http://namespaces.my-example-book-info.com"
    xmlns:xsd="http://www.w3.org/1999/XMLSchema">

    <xsd:element name="GetBookPrice">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element name="ISBN" type="string"/>
          <xsd:element name="Title" type="string"/>
          <xsd:element name="NumPages" type="integer"/>
        </xsd:sequence>
      </xsd:complexType>
    </xsd:element>

    <xsd:element name="GetBookPriceResponse">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element name="CurrentPrice" type="decimal" />
          <xsd:element name="Currency" type="string" />
        </xsd:sequence>
      </xsd:complexType>
    </xsd:element>

  </xsd:schema>
</wsdl:types>

但是WSDL还包含更多信息,关于哪些功能链接在一起以进行操作,以及服务中可用的操作以及网络上可以访问服务/操作的位置。

另见W3 Annotated WSDL Examples


#2 热门回答(72 赞)

SOAP消息是用于传输数据的XML文档。 WSDL是一个XML文档,它描述了如何连接和向Web服务发出请求。

基本上,SOAP消息是你传输的数据,WSDL告诉你可以执行的操作以及如何进行调用。

在谷歌快速搜索将产生许多来源以供额外阅读(之前的书籍链接已经死亡,为了打击这将在评论中提出任何新建议)

只是注意到你的具体问题:

**所有SOAP消息都是WSDL的吗?**不,它们根本不是一回事。

**SOAP是一种接受自己的"SOAP消息"还是"WSDL"的协议?**No - 需要阅读,因为这远远不够。

**如果它们不同,那么我什么时候应该使用SOAP消息?何时应该使用WSDL?**Soap是你应用于你的消息/数据以进行传输的结构。 WSDL仅用于确定如何首先调用服务。当你首次添加代码以调用特定Web服务时,这通常是一次性的事情。


#3 热门回答(25 赞)

WSDL(Web服务定义语言)是描述Web服务的元数据文件。

操作名称,参数等等

soap消息是实际的有效负载


原文链接