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

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

 

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

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

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



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

CREATE PROCEDURE sp_echo2(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;

执行以下任务,使用 C# 或 Python 访问 XML Web 服务:

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

    编写访问 SampleXMLService Web 服务的代码。

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



      using System;
      using System.Xml;
      
      public class WebClient
      {
          static void Main(string[] args)
          {
              XmlTextReader reader = new XmlTextReader(
                  "http://localhost:8082/SampleXMLService?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_numeric = attrs.getValue( 'ret_f' )
                  print('%s %s %s' % ( table_int, table_string, table_numeric ))
      
      parser = xml.sax.make_parser()
      parser.setContentHandler( DocHandler() )
      parser.parse('http://localhost:8082/SampleXMLService?i=5&f=3.14&s=hello')

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

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

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

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

      python DocHandler.py

应用程序显示以下输出:

5 hello 3.14
 另请参见