Translate to:
Watch a FREE one-hour video on Delphi Web Services

Web Services - Introduction

Web services are application blocks that can be published online, and made available to the rest of the world for consumption. Instead of reinventing the wheel and recreating the same solution by multiple organizations, these organizations can access and use a prebuilt application component, and considerably speed up their development cycles. For example, if you have a need in your application to get and analyze stock quotes, you can incorporate a prebuilt web service in your application that handles the requests and presents data that your application can then use as needed.

Web services use open protocols to allow applications to communicate. They are self-contained and self-describing and do not depend on a specific language or platform. Web services allow Windows servers to 'talk' with Unix servers, for C++ coders to use code written in Delphi or VB and vice versa.

Web services depend on XML (Extensible Markup Language) to code and decode your data and SOAP (Simple Object Access Protocol) to transport it. The Description Language (WSDL) is XML-based and it describes the web service and how to access it. The Universal Description, Discovery and Integration (UDDI) is a directory service that allows businesses to register and locate published web services. These primary layers form the building blocks that make up a web service. The following pyramid illustrates the main areas:

Web Services Client Example Using Delphi

In this example, I will walk you through creating a simple Delphi client that consumes an existing weather web service.

Design

Create a new Delphi application project. Drop a PageControl on the form and add two pages to it: Max/Min Temperatures and SOAP Data. On the first page, drop a StringGrid, four Labels and a BitBtn. On the second page, drop two Memos and two labels. On the main form, drop three more Labels, an Edit field and a Button. From the WebServices page on the Tool Palette, choose a THTTPRIO component and drop it on the form. Configure the StringGrid to display 3 columns and 7 rows with 1 row and no columns set as fixed. Position and update the captions of the labels. Refer to the DFM file that is included in the project code for more details.Your form should look as follows:

On the first tabsheet, we will display maximum and minimum temperatures for six days based on a given zip code. We will also display Latitude and Longitude coordinates. On the second tabsheet, we will show the SOAP package that is sent and the one that is returned.

The WSDL

One site you should become familiar with is the XMethods website (www.xmethods.com). The site lists a number of published web services created by many different languages. We will go to that site and look for our web service to locate its WSDL which we will need for our client application. Browse to www.xmethods.com, and then click on "FULL LIST" to view all the listings instead of the recent ones. Do a search for "US Weather" and click on its link. This will take you to the page that describes the web service, including the path for the WSDL file that we will be using in our example. The WSDL file for our weather service is available at: http://www.webservicex.net/WeatherForecast.asmx?WSDL. Below is a snapshot of the first part of this WSDL file:

Notice that it is written in XML. Its purpose is to describe the weather web service and how it should be used. If you examine the content closely, you will find that it gives us the following information about our web service:

  1. The location of the web service (the URL)
  2. The methods it uses, along with their expected parameters
  3. The results of the methods and type of data returned

So, how do we use that in our application?

Delphi comes with a WSDL import wizard that converts the WSDL file to PASCAL code. Click on File, New, Other and browse to the WebServices folder under Delphi Projects:

Double-click on the WSDL Importer in the right window pane. This will launch the import wizard. Paste the path of the WSDL (URL from www.xmethods.com site) directly in the field that prompts you for its location, and click on Finish.

The wizard will convert the WSDL file and provide you with a Code Preview window seen below. Click on Finish, and you will have a PAS file that includes all the methods described in the WSDL file. Refer to the file WeatherForecast.pas included in the project code to see the final result.

Save your new file and then add it to the Uses section in your main form:

implementation

uses WeatherForecast;

{$R *.dfm}

I will not go over all the code here, but I will cover the most important parts. You can download and review the example for all details.

The most important component in our client example is the THTTPRIO ("HTTP Remote Interfaced Object). It reads the description of the required web service interface, generates the Virtual Method Table (VMT) and adapter methods, and returns a reference to the created VMT. This gives us a robust and powerful architecture on the client side and an easy way to take advantage of web services.

Under Object Inspector, paste the WSDL path in the "WSDLLocation" property. The component will parse the WSDL and automatically populate the drop-down boxes for "Service" and "Port." Set those by selecting them as seen below:

Next, create an OnClick Event for the "Check Weather" button. Then define a few variables to help us access the data. Note that in this example, the data is returned in an array and we will therefore need to define a variable to match the returned data type.

var

forecasts : WeatherForecastS;
weatherdata : ArrayOfWeatherData;
i, j : integer;

The main code for the OnClick event is as follows:

forecasts := (HTTPRIO1 as WeatherForecastSoap).GetWeatherByZipCode(eZip.Text);

if forecasts.StateCode <> '' then

Begin

weatherdata := forecasts.Details;
lPlace.Caption := forecasts.PlaceName + ', ' + forecasts.StateCode;
lLat.Caption := floattostr(forecasts.Latitude);
lLong.Caption := floattostr(forecasts.Longitude);
for i := 0 to high(weatherdata) do
With sgDetails do

Begin

Cells[0,i+1] := weatherdata[i].Day;
Cells[1,i+1] := weatherdata[i].MaxTemperatureF;
Cells[2,i+1] := weatherdata[i].MinTemperatureF;

End;

End;

To see the SOAP messages, we will take advantage of two events that are available to us with the THTTPRIO component:

  1. OnBeforeExecute: To show the request
  2. OnAfterExecute: To show the response

The code for these two events is as follows. Notice that the request is WideString, but the response is a stream.

procedure TfrmMain.HTTPRIO1AfterExecute(const MethodName: string; SOAPResponse: TStream);
begin

SoapResponse.Position := 0;
mResponse.Lines.LoadFromStream(SoapResponse);

end;

procedure TfrmMain.HTTPRIO1BeforeExecute(const MethodName: string; var SOAPRequest: WideString);
begin

mRequest.Lines.Text := SoapRequest;

end;

Compile the application and test it.

Here's the SOAP Request:

And here's the first part of the SOAP Response. Notice that an image URL is also returned that you can display as part of the results.

The above has been a brief coverage of how to create a Web Service client using Delphi. For detailed coverage of Web Services using Delphi, please contact us to find out more about our Online Delphi Classes. If you need consulting services, please call at +1 678-921-0644 or visit us online at:

http://www.onlinedelphitraining.com/

 


Do you have any comments about this topic?

Click on: Post Comments/Questions

To download the project click on: Delphi Tip Download

Online Delphi Training


Buy Full Web Services Video Now

Learn LIVE
From The Comfort Of Your Home
Start Immediately!

+1 (678) 921-0644


©2005-2007 Business-IQ