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 Server - Programming » HTTP web services » Access to web services using web clients

 

Quick start to accessing a SQL Anywhere HTTP web server

This section illustrates how to access a SQL Anywhere HTTP web server using two different types of client application — Python and C#. It does not illustrate SQL Anywhere web service application capabilities to a full extent. Many SQL Anywhere web service features are available that are beyond the scope of this topic.

You can develop SQL Anywhere web client applications that connect to any type of online web server, but this guide assumes that you have started a local SQL Anywhere HTTP server on port 8082 and want to connect to a web service named SampleXMLService, created with the following SQL statements:



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;

Perform the following tasks to access an XML web service using C# or Python:

  1. Create a procedure that connects to a web service on an HTTP server.

    Write code that accesses the SampleXMLService web service.

    • For C#, use the following code:



      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();
          }
      }

      Save the code to a file named DocHandler.cs.

      To compile the program, run the following command at a command prompt:

      csc /out:DocHandler.exe DocHandler.cs
    • For Python, use the following code:



      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')

      Save the code to a file named DocHandler.py.

  2. Perform operations on the result set sent by the HTTP server.

    • For C#, run the following command:

      DocHandler
    • For Python, run the following command:

      python DocHandler.py

The application displays the following output:

5 hello 3.14
 See also