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

SQL Anywhere 12.0.0 (中文) » SQL Anywhere 服务器 - 编程 » HTTP Web 服务 » 使用 Web 客户端访问 Web 服务

 

访问 SQL Anywhere HTTP Web 服务器快速入门指南

本快速入门指南介绍了如何使用 Python 和 C# 这两种不同的客户端应用程序来访问 SQL Anywhere HTTP Web 服务器。它不会全面地介绍 SQL Anywhere 的 Web 服务应用程序功能。虽然许多 SQL Anywhere Web 服务功能都可用,但超出了本指南的范畴。

执行以下任务:

  • 创建连接 HTTP 服务器上的 Web 服务的过程。

  • 在 HTTP 服务器发送的结果集上执行操作。

您可以开发连接任何类型在线 Web 服务器的 SQL Anywhere Web 客户端应用程序,但本指南假定您在 8082 端口上启用了一个本地 SQL Anywhere HTTP 服务器,并打算连接由以下 SQL 脚本创建的名为 SampleWebService 的 Web 服务:



CREATE SERVICE SampleWebService
    TYPE 'XML'
    USER DBA
    AUTHORIZATION OFF
    AS CALL sp_echo(:i, :f, :s);

CREATE PROCEDURE sp_echo(i INTEGER, f NUMERIC(6,2), s LONG VARCHAR )
RESULT( ret_i INTEGER, ret_f NUMERIC(6,2), ret_s LONG VARCHAR )
BEGIN
	SELECT i, f, s;
END;

有关快速设置 HTTP 服务器的详细信息,请参见将 SQL Anywhere 用作 HTTP Web 服务器快速入门指南

 ♦  使用 C# 或 Python 访问 XML Web 服务
  1. 编写访问 SampleWebService Web 服务的代码。

    • 对于 C#,使用以下代码:



      using System;
      using System.Xml;
      
      public class WebClient {
          static void Main(string[] args) {
              XmlTextReader reader = new XmlTextReader( "http://localhost:8082/demo/SampleWebService?i=5&f=3.14&s=hello" );
      
              while( reader.Read() ) {
                  switch( reader.NodeType ) {
                      case XmlNodeType.Element:
                          if( reader.Name == "row" ) {
                              Console.Write(reader.GetAttribute("ret_i") + " ");
                              Console.Write(reader.GetAttribute("ret_s") + " ");
                              Console.WriteLine(reader.GetAttribute("ret_f"));
                          }
                          break;
                  }
              }
              reader.Close();
          }
      }

      将此代码保存在名为 DocHandler.cs 的文件中。

      要编译程序,在命令提示符下运行以下命令:

      csc /out:DocHandler.exe DocHandler.cs
    • 对于 Python,使用以下代码:



      import xml.sax
      
      class DocHandler( xml.sax.ContentHandler ):
          def startElement( self, name, attrs ):
              if name == 'row':
                  table_int = attrs.getValue( 'ret_i' )
                  table_string = attrs.getValue( 'ret_s' )
                  table_float = attrs.getValue( 'ret_f' )
                  print '%s %s %s' % ( table_int, table_string, table_float )
      
      parser = xml.sax.make_parser()
      parser.setContentHandler( DocHandler() )
      parser.parse( 'http://localhost:8082/demo/SampleWebService?i=5&f=3.14&s=hello' )

      将此代码保存在名为 DocHandler.py 的文件中。

  2. 运行应用程序。

    • 对于 C#,运行以下命令:

      DocHandler
    • 对于 Python,运行以下命令:

      python DocHandler.py

应用程序显示以下输出:

5 hello 3.14
 另请参见