Delphi Clinic C++Builder Gate Training & Consultancy Delphi Notes Weblog Dr.Bob's Webshop
Bob Swart (aka Dr.Bob) - Medical Officer TDMWeb Kylix Developer's Guide
 Kylix 2 BizSnap - XML, SOAP & WebServices (5 of 6)
See Also: Kylix Papers and Columns

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.


BizSnap is the name given by Borland to describe the feature set of Kylix 2 Enterprise that covers XML document programming as well as Web Services (using SOAP). In this chapter, we will first cover XML document programming using the TXMLDocument component. This will be enhanced using XML Data Binding and even further using the XML Mapping Tool.
We then move on to Web Services, and after a short introduction to SOAP, we'll implement our first Web Service in Kylix 2, followed by the usage (consuming) of Web Services written in Kylix or other development environments. Finally, we show the powerful combination of BizSnap with DataSnap to produce distributed applications (even cross-platform, considering that Delphi 6 also supports this on Windows).

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).

Figure 17: Web Services Importer

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).

Figure 18: Web Services Importer (Advanced)

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.
Save the generated import unit in file CmInch.pas, and add it to the uses clause of the MainForm unit. The generated unit CmInch.pas has the following contents:
  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).
Now, move back to the (empty) main form, and add the generated CmInch.pas unit to the uses clause. Drop two TEdit components (call them edCm and edInch) and two TButton components (call them btnCm2Inch and btnInch2Cm). Finally, you need to drop a THTTPRIO component from the Web Services tab of the Component Palette. The THTTPRIO component is a Remote Invokable Object that communicates using HTTP (hence the name HTTPRIO). It will be used by a client application to connect to a Web Service and pretend to implement the Web Services at the client location. To the client, it's as if the THTTPRIO component is behaving as if it's a local implementation, while in fact the THTTPRIO is connecting to the remote Web Service, sending SOAP requests and receiving SOAP answers over HTTP.
This may sound complex, but working with the THTTPRIO component is really easy. Set the WSDLLocation property of the THTTPRIO component to the location of the WSDL, which can be found at either http://localhost/cgi-bin/K2WebService/wsdl/ICmInch or http://www.drbob42.co.uk/cgi-bin/K2WebService/wsdl/ICmInch (note that the latter is much slower because it requires your THTTPRIO to connect to the Internet for every SOAP request). Now, set the Service property of the THTTPRIO component to ICMInchservice (the only choice if you click on the arrow for the drop-down combo box). And finally set the Port property to ICMInchPort (again the only possible choice). This should enable the design of the Web Service client form, which can be seen in Figure 19.

Figure 19: Centimeters to inch converter Web Service client

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.
This ends the topic of the regular Web Services in Kylix 2 Enterprise. The final section of this chapter will cover the combination of Web Services and DataSnap, with the SOAP Data Module and SOAPConnection component.

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...


This webpage © 2002-2009 by Bob Swart (aka Dr.Bob - www.drbob42.com). All Rights Reserved