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 » HTTP web service examples » Tutorial: Create a web server and access it from a web client

 

Lesson 2: Sending requests from a web client and receiving responses

In this lesson, you set up a database client to send requests to a web server using the POST method and to receive the web server's responses.

Prérequis

This lesson assumes that you have set up a web server as instructed in Lesson 1. For more information about setting up a database server to receive the requests from the web client described in this lesson, see Lesson 1: Setting up a web server to receive requests and send responses.

This lesson assumes that you have the roles and privileges listed in the Privileges section at the start of this tutorial: Tutorial: Create a web server and access it from a web client.

Contexte et remarques

This lesson contains several references to localhost. Use the host name or IP address of the web server from Lesson 1 instead of localhost if you are not running the web client on the same computer as the web server.

 Task
  1. Create a SQL Anywhere database that will be used to contain web client procedures.

    dbinit -dba DBA,sql echo_client
  2. Start a network database server using this database. This server will act as a web client.

    dbsrv16 echo_client.db
  3. Connect to the database server with Interactive SQL.

    dbisql -c "UID=DBA;PWD=sql;SERVER=echo_client"
  4. Create a new stored procedure to send requests to a web service.

    CREATE OR REPLACE PROCEDURE SendWithMimeType( 
        value LONG VARCHAR, 
        mimeType LONG VARCHAR, 
        urlSpec LONG VARCHAR
    )
    URL '!urlSpec'
    TYPE 'HTTP:POST:!mimeType';

    The SendWithMimeType procedure has three parameters. The value parameter represents the body of the request that should be sent to the web service. The urlSpec parameter indicates the URL to use to connect to the web service. The mimeType indicates which MIME type to use for the HTTP:POST.

  5. Send a request to the web server and obtain the response.

    CALL SendWithMimeType('<hello>this is xml</hello>', 
        'text/xml', 
        'http://localhost:8082/EchoService'
    );

    The http://localhost:8082/EchoService string indicates that the web server runs on localhost and listens on port 8082. The desired web service is named EchoService.

  6. Try a different MIME type and observe the response.

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

Résultat

A web client is set up to send HTTP requests to a web server using the POST method and receive the web server's response.

Exemple

The following is an example of a result set displayed by 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

The following is representative of the HTTP packet that is sent to the web server:



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>

The following is the response from the web server:

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>

 See also