PDA

View Full Version : Consuming a Web Service



cairnswm
12-11-2004, 02:13 PM
Before reading this tutorial you should have gone through


Creating a Web Service (http://terraqueous.f2o.org/dgdev/viewtopic.php?p=9727)

cairnswm
12-11-2004, 02:16 PM
Create a New Application

To consume a web service you first create a normal Delphi Application:

File - New - Application

This creates a new application with a form etc.

For this tutorial we need to add two editboxes and two buttons (Change the captions of the buttons to Button1 = set, button2 = get)

Create the Interfacing Unit
The next step is to create the interfacing unit between the applicaiton and the Web service:

File - New - Other, Go to the Web Services tab - Select the WSDL importer.

In the edit box enter the link to the WSDL file of your web service:
http://localhost/cgi-bin/WebServiceServer.exe/wsdl/ITMyWebService

Select Next
Select Finish

This creates unit unit ITMyWebService1; that has everything you need to use the web service.

Instead of explaining how the unit is structured I'll just show you how to use it. First add the unit into the forms uses

clause:

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ITMyWebService1;


Declaring the Web Service
In the form of the application we need to declare an instance of the web service.

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
bGet: TButton;
bSet: TButton;
private
{ Private declarations }
WC : ITMyWebService;
public
{ Public declarations }
end;


Using the Web service

In the forms OnCreate we link the instance of the web service to the online web service:


procedure TForm1.FormCreate(Sender: TObject);
begin
WC := GetITMyWebService;
end;


This single statement does all the marshalling and remote invokation and all those other good web technology things. For us all that matters is we now know about the Web service and can access it as needed. So lets set and get our names:

procedure TForm1.bGetClick(Sender: TObject);
begin
Edit2.Text := WC.GetName;
end;

procedure TForm1.bSetClick(Sender: TObject);
begin
WC.SetName(Edit1.Text);
end;


Testing the Web Service:

Ok so run the application. Press Get, then Press Set, then press Get.

When you press the first Get the second edit box should show William (Or what ever default you created in your Web Service's contructor).
When you click set - nothing should happen (in the back ground the server is being updated with the value in Edit1 - probably Edit1 unless you changed it).

The second press of get should change the text in the second edit to be the same as in the first edit - this is the value retrieved from the server. If you open the ini file created on your computer you will see that it has been set to Edit1 as well (So if you repeat the above steps you arn't going to see very much).