在本课中,会将数据库客户端设置为使用 POST 方法向 Web 服务器发送请求并接收 Web 服务器的响应。
前提条件
本课假设您已经按照第 1 课中说明的方式设置了 Web 服务器。有关本课所述的设置数据库服务器以接收 Web 客户端请求的详细信息,请参见第 1 课:设置用于接收请求和发送响应的 Web 服务器。
本课假定您拥有在教程教程:创建一个 Web 服务器并从 Web 客户端访问此 Web 服务器开头的特权部分中列出的角色和特权。
上下文和注释
本课中包含对 localhost 的引用。如果 Web 客户端与 Web 服务器运行在不同的计算机上,则使用第 1 课中 Web 服务器的主机名或 IP 地址而不是 localhost。
创建将用于包含 Web 客户端过程的 SQL Anywhere 数据库。
dbinit -dba DBA,sql echo_client |
使用此数据库启动网络数据库服务器。此服务器将充当 Web 客户端。
dbsrv16 echo_client.db |
使用 Interactive SQL 连接到数据库服务器。
dbisql -c "UID=DBA;PWD=sql;SERVER=echo_client" |
创建新的存储过程以向 Web 服务发送请求。
CREATE OR REPLACE PROCEDURE SendWithMimeType( value LONG VARCHAR, mimeType LONG VARCHAR, urlSpec LONG VARCHAR ) URL '!urlSpec' TYPE 'HTTP:POST:!mimeType'; |
SendWithMimeType 过程具有三个参数。value 参数表示应发送到 Web 服务的请求正文。urlSpec 参数表示用于连接到 Web 服务的 URL。mimeType 表示 HTTP:POST 所用的 MIME 类型。
将请求发送到 Web 服务器并获得响应。
CALL SendWithMimeType('<hello>this is xml</hello>', 'text/xml', 'http://localhost:8082/EchoService' ); |
http://localhost:8082/EchoService 字符串表示 Web 服务器运行在 localhost 上并监听 8082 端口。所需的 Web 服务名为 EchoService。
尝试其它 MIME 类型并观察响应。
CALL SendWithMimeType('{"menu": { "id": "file", "value": "File", "popup": { "menuitem": [{"value": "New", "onclick": "CreateNew()"}, {"value": "Open", "onclick": "Open()"}, {"value": "Close", "onclick": "Close()"} ] } } }', 'application/json', 'http://localhost:8082/EchoService' ); |
例
以下是由 Interactive SQL 显示的结果集示例:
Attribute | Value |
---|---|
Status | HTTP/1.1 200 OK |
Body | <hello>this is xml</hello> |
Server | SQLAnywhere/16.0.1234 |
Expires | Tue, 09 Oct 2012 21:06:01 GMT |
Date | Tue, 09 Oct 2012 21:06:01 GMT |
Content-Type | text/xml; charset=windows-1252 |
Connection | close |
以下是发送到 Web 服务器的 HTTP 包的代表示例:
POST /EchoService HTTP/1.0 ASA-Id: 46758096650a44088c77237cc8719d5c User-Agent: SQLAnywhere/16.0.1234 Accept-Charset: windows-1252, UTF-8, * Date: Tue, 09 Oct 2012 21:06:01 GMT Host: localhost:8082 Connection: close Content-Type: text/xml; charset=windows-1252 Content-Length: 26 <hello>this is xml</hello> |
以下是来自 Web 服务器的响应:
HTTP/1.1 200 OK Date: Tue, 09 Oct 2012 21:06:01 GMT Connection: close Expires: Tue, 09 Oct 2012 21:06:01 GMT Content-Type: text/xml; charset=windows-1252 Server: SQLAnywhere/16.0.1234 <hello>this is xml</hello> |
![]() |
使用DocCommentXchange讨论此页。
|
版权 © 2013, SAP 股份公司或其关联公司. - SAP Sybase SQL Anywhere 16.0 |