Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 12.0.1 » SQL Anywhere 服务器 - 编程 » HTTP Web 服务 » HTTP Web 服务示例 » 教程:使用 SQL Anywhere 访问 SOAP/DISH 服务

 

第 3 课:发送 SOAP 请求和接收 SOAP 响应

在本课中,将调用在上一课中创建的包装过程,它会向第一课中创建的 Web 服务器发送 SOAP 请求。有关本课所述的设置 Web 客户端以发送 SOAP 请求的详细信息,请参见第 2 课:设置用于发送 SOAP 请求和接收 SOAP 响应的 Web 客户端.

 ♦ 发送 SOAP 请求
  1. 如果在第二课中没有打开客户端数据库,则在 Interactive SQL 中连接它。

    dbisql -c "UID=DBA;PWD=sql;SERVER=ftc_client"
  2. 启用 SOAP 请求和响应的记录。

    在 Interactive SQL 中执行以下 SQL 语句:

    CALL sa_server_option('WebClientLogFile', 'soap.txt');
    CALL sa_server_option('WebClientLogging', 'ON');

    这些调用用于检查 SOAP 请求和响应的内容。请求和响应均记录在名为 soap.txt 的文件中。

  3. 调用该包装过程以发送 SOAP 请求并接收 SOAP 响应。

    在 Interactive SQL 中执行以下 SQL 语句:

    CALL FahrenheitToCelsius(212);

    本次调用将华氏温度值 212 传递给 FahrenheitToCelsius 过程,而此过程将值和两个自定义的 SOAP 标头传递给 FToC 过程。两个客户端过程都在上一课中创建。

FToC Web 服务过程向 Web 服务器发送华氏温度值和 SOAP 标头。SOAP 请求包含如下内容。



<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:m="http://localhost:8082">
  <SOAP-ENV:Header>
    <Authentication xmlns="SecretAgent" mustUnderstand="1">
      <userName alias="99">
         <first>Susan</first>
         <last>Hilton</last>
      </userName>
    </Authentication>
    <Session xmlns="SomeSession">
      123456789
    </Session>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <m:FtoCService>
      <m:temperature>212</m:temperature>
    </m:FtoCService>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

然后,FtoC 过程接收 Web 服务器的响应,此响应包括基于华氏温度值的结果。SOAP 响应包含如下内容。



<SOAP-ENV:Envelope 
  xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
  xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' 
  xmlns:tns='http://localhost:8082'>
  <SOAP-ENV:Header>
    <Authentication xmlns="SecretAgent" alias="99" mustUnderstand="1">
      <first>Susan</first>
      <last>Hilton</last>
    </Authentication>
  </SOAP-ENV:Header> 
  <SOAP-ENV:Body>
    <tns:FtoCServiceResponse>
      <tns:FtoCServiceResult xsi:type='xsd:string'>
        &lt;tns:rowset xmlns:tns=&quot;http://localhost:8082/ftc&quot;&gt;&#x0A; 
        &lt;tns:row&gt;&#x0A;  
        &lt;tns:answer&gt;20
        &lt;/tns:answer&gt;&#x0A; 
        &lt;/tns:row&gt;&#x0A;
        &lt;/tns:rowset&gt;&#x0A;   
      </tns:FtoCServiceResult>
      <tns:sqlcode>0</tns:sqlcode>
    </tns:FtoCServiceResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

<SOAP-ENV:Header> 的内容在 inoutheader 中返回。

如果检查 SOAP 响应,则可以看出由 FToCService Web 服务在响应中对结果集编码。将结果集解码并返回给 FahrenheitToCelsius 过程。将华氏温度值 212 传递给 Web 服务器时,结果集类似于如下内容:

<tns:rowset xmlns:tns="http://localhost:8082/ftc"> 
  <tns:row>
    <tns:answer>100
    </tns:answer>
  </tns:row>
</tns:rowset>

FahrenheitToCelsius 过程中的 SELECT 语句使用 OPENXML 函数来解析 SOAP 响应,从中提取由 tns:answer 结构定义的摄氏温度值。

在 Interactive SQL 中生成以下结果集:

temperature Celsius
212 100
 另请参见