|Delphi Clinic||C++Builder Gate||Training & Consultancy||Delphi Notes Weblog||Dr.Bob's Webshop|
I have been working on an BizSnap chapter for the Kylix Developer's Guide - adding more coverage of Kylix 2 to this book. The BizSnap chapter will be published on my website (in six weekly parts), covering XML Document Programming and Web Services support.
5. Consuming Web Services
A Web Service consumer can be any kind of application. But to keep things simple, let's just start a new default visual application in Kylix. Save the form in file MainForm.pas and the project in K2WSClient.dpr.
Web Service Importer
This time, you need to use the Web Service Importer because you must import the WSDL that defines the Web service (interface) and generate an Object Pascal import unit that defines the Object Pascal interface of this particular Web service. So choose File | New, and go to the WebServices tab of the Object Repository and double-click on the Web Service Importer icon, which will result in the Web Services Import Wizard (see Figure 17).
Enter the URL of the Web Service that you've just created. This can be http://localhost/cgi-bin/K2WebService/wsdl/ICmInch (on your own localhost machine) or the "real" http://www.drbob42.co.uk/cgi-bin/K2WebService/wsdl/ICmInch (for the version deployed on my Web site on the Internet).
After you've specified the URL to retrieve the WSDL information for your Web Service, which was http://www.drbob42.co.uk/cgi-bin/K2WebService/wsdl/ICmInch, you can take a look at the options in the Advanced tab, or directly click on the Generate button to create the Web Service import unit. Note that the Web Services Importer will also work with Web Services that are written in an entirely different development environment, although some interoperability issues between SOAP implementations (in other environments) may remain to be worked on.
Unit CmInch; interface uses Types, XSBuiltIns; type ICmInch = interface(IInvokable) function Cm2Inch(const Inch: Double): Double; stdcall; function Inch2Cm(const Cm: Double): Double; stdcall; end; implementation uses InvokeRegistry; initialization InvRegistry.RegisterInterface(TypeInfo(ICmInch), 'urn:CentimeterInchIntf-ICmInch', ''); end.This looks a lot like the CentimeterInchIntf.pas unit you wrote in the previous section (for the Web Service server). Which isn't too strange, of course, if you consider that the purpose of the Web Services Importer Wizard is to indeed regenerate the interface (in Object Pascal code).
You now need to write the code for the Cm2Inch and Inch2Cm buttons. Both have a similar implementation that starts by using the THTTPRIO component and extracting the ICmInch interface from it. After that, you can use the methods Cm2Inch and Inch2Cm from this interface, as if they were simple local methods. See the following listing:
procedure TForm1.btnCm2InchClick(Sender: TObject); var Cm: Double; begin Cm := StrToFloatDef(edCm.Text,0); edInch.Text := FloatToStr((HTTPRIO1 as ICmInch).Cm2Inch(Cm)) end; procedure TForm1.btnInch2CmClick(Sender: TObject); var Inch: Double; begin Inch := StrToFloatDef(edInch.Text,0); edCm.Text := FloatToStr((HTTPRIO1 as ICmInch).Inch2Cm(Inch)) end;After you compile and run the client application, you can enter inches and convert them to centimeters (or vice versa). Or use Kylix to consume one of the numerous other available Web Services or think and produce another Web Service yourself. The sky and your imagination are the limit.
Next Time, Dr.Bob says...
In the next and last section of this chapter, we'll combine DataSnap with SOAP and Web Services to produce multi-tier applications using Kylix 2 Enterprise. All this and more next week, so stay tuned...