在本课中,您将设置新的数据库服务器并创建 SOAP 服务来处理进来的 SOAP 请求。服务器预计提供需要转换为摄氏温度的华氏温度值的 SOAP 请求。
运行以下命令创建 SQL Anywhere 数据库:
dbinit ftc |
使用以下命令启动网络数据库服务器:
dbsrv12 -xs http(port=8082) -n ftc ftc.db |
此命令表示 HTTP Web 服务器应当监听 8082 端口上的请求。如果网络禁用了 8082 端口,则使用其他端口号。
使用以下命令来连接 Interactive SQL 中的数据库服务器:
dbisql -c "UID=DBA;PWD=sql;SERVER=ftc" |
创建新的 SOAP 服务来接受进来的请求。
在 Interactive SQL 中运行以下 SQL 脚本:
CREATE SERVICE FtoCService TYPE 'SOAP' FORMAT 'XML' AUTHORIZATION OFF USER DBA AS CALL FToCConvertor( :temperature ); |
此脚本创建新的名为 FtoCService 的 SOAP 服务,它生成 XML 格式的字符串作为输出。当 Web 客户端向服务发送 SOAP 请求时,它调用名为 FToCConvertor 的存储过程。在下一个步骤中将创建 FToCConvertor 过程。
创建 FToCConvertor 过程以处理到来的 SOAP 请求。此过程执行必要的运算来将客户端提供的华氏温度值转换为摄氏温度值。
在 Interactive SQL 中运行以下 SQL 脚本:
CREATE PROCEDURE FToCConvertor( temperature FLOAT ) BEGIN DECLARE hd_key LONG VARCHAR; DECLARE hd_entry LONG VARCHAR; DECLARE alias LONG VARCHAR; DECLARE first_name LONG VARCHAR; DECLARE last_name LONG VARCHAR; DECLARE xpath LONG VARCHAR; DECLARE authinfo LONG VARCHAR; DECLARE namespace LONG VARCHAR; DECLARE mustUnderstand LONG VARCHAR; header_loop: LOOP SET hd_key = NEXT_SOAP_HEADER( hd_key ); IF hd_key IS NULL THEN -- no more header entries LEAVE header_loop; END IF; IF hd_key = 'Authentication' THEN SET hd_entry = SOAP_HEADER( hd_key ); SET xpath = '/*:' || hd_key || '/*:userName'; SET namespace = SOAP_HEADER( hd_key, 1, '@namespace' ); SET mustUnderstand = SOAP_HEADER( hd_key, 1, 'mustUnderstand' ); BEGIN -- parse the XML returned in the SOAP header DECLARE crsr CURSOR FOR SELECT * FROM OPENXML( hd_entry, xpath ) WITH ( alias LONG VARCHAR '@*:alias', first_name LONG VARCHAR '*:first/text()', last_name LONG VARCHAR '*:last/text()' ); OPEN crsr; FETCH crsr INTO alias, first_name, last_name; CLOSE crsr; END; -- build a response header -- based on the pieces from the request header SET authinfo = XMLELEMENT( 'Authentication', XMLATTRIBUTES( namespace as xmlns, alias, mustUnderstand ), XMLELEMENT( 'first', first_name ), XMLELEMENT( 'last', last_name ) ); CALL SA_SET_SOAP_HEADER( 'authinfo', authinfo ); END IF; END LOOP header_loop; SELECT ROUND((temperature - 32.0) * 5.0 / 9.0, 5) AS answer; END; |
NEXT_SOAP_HEADER 函数用于 LOOP 结构中再次遍历 SOAP 请求中的所有标头名,并在 NEXT_SOAP_HEADER 函数返回 NULL 时退出循环。
此函数不一定按照标头在 SOAP 请求中出现的顺序遍历它们。
SOAP_HEADER 函数在标头名不存在时返回标头值或 NULL。FToCConvertor 过程搜索名为 Authentication 的标头,并抽取标头结构,包括 @namespace 和 mustUnderstand 属性。
以下为可能的 Authentication 标头的 XML 字符串表示,其中 @namespace 属性值为 "SecretAgent",且 mustUnderstand 值为 1:
<Authentication xmlns="SecretAgent" mustUnderstand="1"> <userName alias="99"> <first>Susan</first> <last>Hilton</last> </userName> </Authentication> |
SELECT 语句中的 OPENXML 系统过程用于抽取 XML 字符串中的信息,其中 xpath 字符串设置为 "/*:Authentication/*:userName",然后在 crsr 变量中存储结果集。有关 OPENXML 系统过程的详细信息,请参见openxml 系统过程。
SET 语句用于构建 XML 格式的 SOAP 响应以发送给客户端。以下是可能的 SOAP 响应的 XML 字符串表示。它基于上述 Authentication 标头结构示例。
<Authentication xmlns="SecretAgent" alias="99" mustUnderstand="1"> <first>Susan</first> <last>Hilton</last> </Authentication> |
SA_SET_SOAP_HEADER 系统过程用于向客户端发送 SOAP 响应。
最后的 SELECT 语句用来将提供的华氏温度值转换为摄氏温度值。信息将被中继回客户端。
![]() |
使用DocCommentXchange 讨论此页。
|
版权 © 2010, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.0 |