Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Consuming Web Service


  • Please log in to reply
1 reply to this topic

#1 Luthfi


    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 01 April 2011 - 07:29 AM

Using or consuming a web service is easy with Delphi. Support for creating and consuming web service has been available since Delphi 6 (2001). There are many interesting web services available to use for free that we can add into our own application. This tutorial will show you how to consume a simple web service inside your Delphi application.

  • First we have to find web service we want to consume. In very serious project, you can create your own web service(s). For our tutorial we will visit www.xmethods.com that lists some web services to see if we can find an interesting one. Once you are there, click the Full list link at the bottom to view all the web services.

    I found this one is interesting: QuoteOfTheDay. It says that this web service "Returns a daily random quote and its author.". Very interesting. Now click its link so we can get the wsdl of this web service. When opening the link, you will get to a page that has link to QuoteOfTheDay's wsdl (at the top, see picture). Let's copy this link.

  • Open Delphi, and create new application, save both project and the main form. Now we want to import the wsdl into Delphi object(s).
  • Click menu File- New - Other. From the dialog, select tab Web service and then select WSDL Importer (see picture below). This will start WSDL import wizard.


    When the wizard started, it will ask for WSDL location to import. Since we have copied this information, just paste into the edit box then click button Next.


    The wizard will show you the preview of the imported WSDL. Click button Finish to finish the wizard and use the imported codes.


    The wizard then will create a new unit with name related with the web service. Now save this unit. In my case, I named the unit QuoteOfTheDay.

    Before moving on, let's inspect the new unit. QuoteOfTheDay unit contains a class named Quotes having two properties, i.e. QuoteOfTheDay and Author. We can safely assume that the web service will return the quotes to our Delphi application using this class.

    Beside the Quotes class, we also have QuoteofTheDaySoap interface. This interface only has one method, i.e. function GetQuote which returns an instance of Quotes class. Then we know that to get quote(s) from the web service we must execute this method, and then read the quote(s) from the returned instance of Quotes class.

    But how can we invoke GetQuote method? Since it's declared in an interface, we can not directly call it. We need an object that supports the interface. Luckily for us, inside the new unit we also have public function GetQuoteofTheDaySoap which returns an object that supports QuoteofTheDaySoap interface. Further inspection on the function's body reveals that the function uses an instance of THTTPRIO and cast it into QuoteofTheDaySoap interface (see if you can find this line: "Result := (RIO as QuoteofTheDaySoap);"). So actually THTTPRIO is our SOAP client. Now we know that in order to consume a web service, we need an instance of THTTPRIO. Btw, THTTPRIO is available in design time (for drag and drop). Find it in component pallette in tab Web Services.

    Let's move on with our project.
  • Drop a TLabel onto our main form. Adjust its size and location. Name this TLabel as lblQuote.
  • Drop a TButton or TBitBtn onto the main form. Adjust its location to be below lblQuote. Set its caption into "Refresh Now!".
  • Add QuoteOfTheDay unit into our uses list.
  • Add a function method named GetRandomQuote which returns string into protected section of the main form. Use the following codes for its implementation:

    function TForm1.GetRandomQuote: string;
      vQuoteSoap: QuoteofTheDaySoap;
      vQuote: Quotes;
      vQuoteSoap := GetQuoteofTheDaySoap;
      vQuote := vQuoteSoap.GetQuote;
        Result := Format('"%s"'#13#10'(%s)', [vQuote.QuoteOfTheDay, vQuote.Author]);
  • Add the following codes for the button's OnClick event

    procedure TForm1.BitBtn1Click(Sender: TObject);
      vQuote: string;
        vQuote := GetRandomQuote;
        lblQuote.Caption := vQuote;
  • I also want that the quote gets updated automatically in 10 seconds interval. So drop a TTimer into our main form, set its interval into 10000 and enable it. Set its OnTimer event handler to point to the button's OnClick event handler. Since they both use same parameter, we can use same event handler method for them.
  • Now we are finished with our web service consumer. Press F9 to compile and run your project. See if you get quotes similar like shown below:


Attached Thumbnails

  • Quote001.png

Attached Files

Edited by LuthfiHakim, 12 April 2012 - 01:27 AM.
grammar, typo

  • 1

#2 kpkpkp


    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 03 April 2014 - 11:52 AM

Thank you for the brief, yet complete example.

  • 0

Also tagged with one or more of these keywords: timer

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download