Mega Code Archive

 
Categories / Delphi / Ide Indy
 

Web Services Made Simple in Delphi 6

Title: Web Services - Made Simple in Delphi 6 Question: How to access a web service from Delphi? How to use SOAP components in Delphi? Answer: 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; interface uses Types, XSBuiltIns; type BNQuotePortType = interface(IInvokable) ['{A37458FD-F89D-4BDF-BED9-1592153A51CB}'] function getPrice(const isbn: WideString): Single; stdcall; end; implementation uses InvokeRegistry; initialization InvRegistry.RegisterInterface(TypeInfo(BNQuotePortType), '', ''); end. 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. BNQuotePrj.dpr program BNQuotePrj; uses Forms, BNQuote in '..\UnitFiles\BNQuote.pas' {Form1}, BNQuoteService in '..\WebServicesUnitFiles\BNQuoteService.pas'; {$R *.res} begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end. unit BNQuote; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Rio, SoapHTTPClient; type TForm1 = class(TForm) HTTPRIO1: THTTPRIO; Button1: TButton; Edit1: TEdit; Label1: TLabel; Edit2: TEdit; Label2: TLabel; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation uses BNQuoteService; {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var QuotePrice : Real; begin if Trim(Edit1.Text) '' then begin QuotePrice := 0; QuotePrice :=(HTTPRIO1 as BNQuotePortType).getPrice(Edit1.Text); if QuotePrice -1 then Edit2.Text := FloatToStr(QuotePrice) else begin MessageDlg('Wrong ISBN Code' + #13 + 'Enter a Valid ISBN code',mtInformation,[mbOk],0); Button2.Click; Edit1.SetFocus; end; end else begin MessageDlg('Enter a Valid ISBN code',mtInformation,[mbOk],0); Edit1.SetFocus; end; end; procedure TForm1.Button2Click(Sender: TObject); begin Edit1.Clear; Edit2.Clear; Edit1.SetFocus; end; end. In the sample application I used two edit boxes, two labels and the THTTPRIO component. The getprice function will accept the ISBN code and return the value of the book price in US $ if the ISBN code is a valid one. If you pass an invalid ISBN code, then the function will return -1. Enjoy!!! Thanks. Magesh.