Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 2 - Creating a Web Service Client with Delphi
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DELPHI-KYLIX

Creating a Web Service Client with Delphi
By: Leidago
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 11
    2006-10-30

    Table of Contents:
  • Creating a Web Service Client with Delphi
  • Code Listing
  • The application
  • Behind the scenes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Creating a Web Service Client with Delphi - Code Listing


    (Page 2 of 4 )

     // ************************************************************* //

    // The types declared in this file were generated from data read from the

    // WSDL File described below:

    // WSDL     : http://www.webservicex.net/WeatherForecast.asmx?WSDL

    // Encoding : utf-8

    // Version  : 1.0

    // (13/08/2006 14:39:01 - 1.33.2.5)

    // ************************************************************* //

    unit WeatherForecast;

    interface

    uses InvokeRegistry, SOAPHTTPClient, Types, XSBuiltIns;

    type

      // ************************************************************* //

      // The following types, referred to in the WSDL document are not being represented

      // in this file. They are either aliases[@] of other types represented or were referred

      // to but never[!] declared in the document. The types from the latter category

      // typically map to predefined/known XML or Borland types; however, they could also

      // indicate incorrect WSDL documents that failed to declare or import a schema type.

      // ************************************************************* //

      // !:string          - "http://www.w3.org/2001/XMLSchema"

      // !:float           - "http://www.w3.org/2001/XMLSchema"

      WeatherData          = class;                 { "http://www.webservicex.net" }

      WeatherForecasts     = class;                 { "http://www.webservicex.net" }

      // ************************************************************** //

      // Namespace : http://www.webservicex.net

      // ************************************************************* //

      WeatherData = class(TRemotable)

      private

        FDay: WideString;

        FWeatherImage: WideString;

        FMaxTemperatureF: WideString;

        FMinTemperatureF: WideString;

        FMaxTemperatureC: WideString;

        FMinTemperatureC: WideString;

      published

        property Day: WideString read FDay write FDay;

        property WeatherImage: WideString read FWeatherImage write FWeatherImage;

        property MaxTemperatureF: WideString read FMaxTemperatureF write FMaxTemperatureF;

        property MinTemperatureF: WideString read FMinTemperatureF write FMinTemperatureF;

        property MaxTemperatureC: WideString read FMaxTemperatureC write FMaxTemperatureC;

        property MinTemperatureC: WideString read FMinTemperatureC write FMinTemperatureC;

      end;

      ArrayOfWeatherData = array of WeatherData;    { "http://www.webservicex.net" }

      // ************************************************************** //

      // Namespace : http://www.webservicex.net

      // ************************************************************* //

      WeatherForecasts = class(TRemotable)

      private

        FLatitude: Single;

        FLongitude: Single;

        FAllocationFactor: Single;

        FFipsCode: WideString;

        FPlaceName: WideString;

        FStateCode: WideString;

        FStatus: WideString;

        FDetails: ArrayOfWeatherData;

      public

        destructor Destroy; override;

      published

        property Latitude: Single read FLatitude write FLatitude;

        property Longitude: Single read FLongitude write FLongitude;

        property AllocationFactor: Single read FAllocationFactor write FAllocationFactor;

        property FipsCode: WideString read FFipsCode write FFipsCode;

        property PlaceName: WideString read FPlaceName write FPlaceName;

        property StateCode: WideString read FStateCode write FStateCode;

        property Status: WideString read FStatus write FStatus;

        property Details: ArrayOfWeatherData read FDetails write FDetails;

      end;

      // ************************************************************** //

      // Namespace : http://www.webservicex.net

      // soapAction: http://www.webservicex.net/%operationName%

      // transport : http://schemas.xmlsoap.org/soap/http

      // style     : document

      // binding   : WeatherForecastSoap

      // service   : WeatherForecast

      // port      : WeatherForecastSoap

      // URL       : http://www.webservicex.net/WeatherForecast.asmx

      // ************************************************************** //

      WeatherForecastSoap = interface(IInvokable)

      ['{45A46EB0-5550-8C38-49AC-AE13AD113F74}']

        function  GetWeatherByZipCode(const ZipCode: WideString): WeatherForecasts; stdcall;

        function  GetWeatherByPlaceName(const PlaceName: WideString): WeatherForecasts; stdcall;

      end;

    function GetWeatherForecastSoap(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): WeatherForecastSoap;

    implementation

    function GetWeatherForecastSoap(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): WeatherForecastSoap;

    const

      defWSDL = 'http://www.webservicex.net/WeatherForecast.asmx?WSDL';

      defURL  = 'http://www.webservicex.net/WeatherForecast.asmx';

      defSvc  = 'WeatherForecast';

      defPrt  = 'WeatherForecastSoap';

    var

      RIO: THTTPRIO;

    begin

      Result := nil;

      if (Addr = '') then

      begin

        if UseWSDL then

          Addr := defWSDL

        else

          Addr := defURL;

      end;

      if HTTPRIO = nil then

        RIO := THTTPRIO.Create(nil)

      else

        RIO := HTTPRIO;

      try

        Result := (RIO as WeatherForecastSoap);

        if UseWSDL then

        begin

          RIO.WSDLLocation := Addr;

          RIO.Service := defSvc;

          RIO.Port := defPrt;

        end else

          RIO.URL := Addr;

      finally

        if (Result = nil) and (HTTPRIO = nil) then

          RIO.Free;

      end;

    end;

    destructor WeatherForecasts.Destroy;

    var

      I: Integer;

    begin

      for I := 0 to Length(FDetails)-1 do

        if Assigned(FDetails[I]) then

          FDetails[I].Free;

      SetLength(FDetails, 0);

      inherited Destroy;

    end;

    initialization

      InvRegistry.RegisterInterface(TypeInfo(WeatherForecastSoap), 'http://www.webservicex.net', 'utf-8');

      InvRegistry.RegisterDefaultSOAPAction(TypeInfo(WeatherForecastSoap), 'http://www.webservicex.net/%operationName%');

      InvRegistry.RegisterInvokeOptions(TypeInfo(WeatherForecastSoap), ioDocument);

      RemClassRegistry.RegisterXSClass(WeatherData, 'http://www.webservicex.net', 'WeatherData');

      RemClassRegistry.RegisterXSInfo(TypeInfo(ArrayOfWeatherData), 'http://www.webservicex.net', 'ArrayOfWeatherData');

      RemClassRegistry.RegisterXSClass(WeatherForecasts, 'http://www.webservicex.net', 'WeatherForecasts');

    end.

    As you can see, the file describes in some detail the service methods and, among other things, the location of the file. We are only interested in two methods and their return types:

    function  GetWeatherByZipCode(const ZipCode: WideString):
    WeatherForecasts; stdcall;

     function  GetWeatherByPlaceName(const PlaceName: WideString):
    WeatherForecasts; stdcall;

    The functions takes a parameter each, either the name or zip code of the city whose weather forecast you want to check. They then return a "weatherforecasts" type. Now if you call the function like so:

    Memo1.lines.Add( getplacebyname(‘London’));

    It won’t work, because the type that is returned is not of the "TString" type, it is of the "weatherforecast" type.  If you take  a closer look at the "WeatherData" class, you will see that all the data is stored in an "ArrayOfWeatherData" array. So when you call either of these functions, the data is stored in the above array.

    More Delphi-Kylix Articles
    More By Leidago


       · In this article i explain how to create a webservice client with Delphi.
     

    DELPHI-KYLIX ARTICLES

    - Delphi Wrapper Classes and XML
    - Delphi and the DOM
    - Delphi and XML
    - Internet Access: Client Service
    - Finishing the Client for an Internet Access ...
    - The Client for an Internet Access Control Ap...
    - User Management for an Internet Access Contr...
    - Important Procedures for an Internet Access ...
    - Server Code for an Internet Access Control A...
    - Constructing the Interface for an Internet A...
    - Building a Server Application for an Interne...
    - Building an Internet Access Control Applicat...
    - Client Dataset: Working with Data Packets an...
    - Using the Client Dataset in an N-Tiered Appl...
    - Using the Client Dataset in Two-Tiered Clien...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT