Web Services - Made Simple in Delphi 6...

How do we access a web service from Delphi?
How do we use SOAP components in Delphi?

You can learn the basics of computer programming and creating software of your own by checking out the excellent Software Requirements Unleashed.  To learn how to access web services using SOAP, read on!

Accessing web services using SOAP...

In Delphi 6, accessing web services using SOAP is made very easy with a complete set of components in the Web Services palette. In this article, I'm going to present you a simple example in Delphi 6 on how to use the web services.

A web service is a service intended to perform a specific task. It could be for example, converting a temperature from Centigrade to Farenheit etc., These services are based on a language called Web Services Description Language(WSDL). It's an XML based language. You can find such WSDL files in http://www.xmethods.net. There are lot of such web services available in that site.

How it works?

For example, if you want to convert a temperature from centigrade to farenheit, then you will probably input the centigrade temperature to that service. Now that input is being prepared as an XML request and being sent to the web service. Then the web service is performing the conversion and sending the result back to the client as an XML response. All these tasks are performed for the client by the WSDL. This is just a broad view on its funtionality.

In this article, I'm going to use a web service to find a book price at Barnes & Noble with the ISBN code. You can find the web service at http://www.xmethods.net/detail.html?id=7.

How can we access this web service from Dephi 6?

1. Download the WSDL file to your local drive.
2. Import the web service into Delphi
This is one of the new features of Delphi 6. Click New and in the dialog box select the WebServices tab and select the Web Services Importer. Another dialog box will come. In that, there will be two tabs. In the Import tab, click on browse and select the WSDL file saved from your local drive. Then Click on Generate button; An unit file will be created with the service details.

The content of the newly created/generated file will be like this:

Unit BNQuoteService;
uses Types, XSBuiltIns;
  BNQuotePortType = interface(IInvokable)
    function getPrice(const isbn: WideString): Single; stdcall;
uses InvokeRegistry;
  InvRegistry.RegisterInterface(TypeInfo(BNQuotePortType), '', '');

Now we can use the function getprice in this unit file to find the book price.

There is a new component THTTPRIO under the WebSevices Palette in Delphi 6. This component will help us invoking the method of the web service. Create a new application and drop that component.

a. Set the WSDLLocation, Sevice, Port properties in the Object Inspector.

Here is the sample application that uses the web service and finds the book price. In the sample application add this unit file.

program BNQuotePrj;
  BNQuote in '..\UnitFiles\BNQuote.pas' {Form1},
  BNQuoteService in '..\WebServicesUnitFiles\BNQuoteService.pas';
{$R *.res}
  Application.CreateForm(TForm1, Form1);
unit BNQuote;
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, Rio, SoapHTTPClient;
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Edit2: TEdit;
    Label2: TLabel;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    { Private declarations }
    { Public declarations }
  Form1: TForm1;
uses BNQuoteService;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
  QuotePrice : Real;
  if Trim(Edit1.Text) <> '' then
     QuotePrice := 0;
     QuotePrice :=(HTTPRIO1 as BNQuotePortType).getPrice(Edit1.Text);
     if QuotePrice <> -1 then
        Edit2.Text := FloatToStr(QuotePrice)
        MessageDlg('Wrong ISBN Code' + #13 + 'Enter a Valid ISBN code',mtInformation,[mbOk],0);
     MessageDlg('Enter a Valid ISBN code',mtInformation,[mbOk],0);

procedure TForm1.Button2Click(Sender: TObject);

In the sample application I used two edit boxes, two labels and the THTTPRIO component.




Share this article!

Follow us!

Find more helpful articles: