Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (15)


Web Services - Made Simple in Delphi 6...Format this article printer-friendly!Bookmark function is only available for registered users!
Accessing web services using SOAP...
Product:
Delphi 6.x (or higher)
Category:
WebSnap
Skill Level:
Scoring:
Last Update:
03/14/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet SOAP,Web Services, THTTPRIO
Times Scored:
14
Visits:
17204
Uploader: S S B Magesh Puvananthiran
Company:
Reference: N/A
 
Question/Problem/Abstract:
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.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
System error
    P K (Jun 19 2006 9:04AM)

I am doing exactly as shown in the example here.(i am using out internal web service) But still i get the error:
"system error -2146697210
Line : 0"

what could be the issue here?
Respond

Other Demo Webservices at xmethods.com
    Feroz Ahanger (Sep 4 2002 7:29AM)

your article is helpful for webservice beginners like me. my client application for this works fine. i tried creating some more client applications for other demos at xmethods.com (for e.g:Currency Exchange Rate). i get error "The Parameter is incorrect". Here is my code for onClick of Btn :
procedure TForm1.Button1Click(Sender: TObject);
var
myExchangeRate : CurrencyExchangePortType;
country1, country2 : WideSTring;
exRate : Single;
begin
myExchangeRate := (HTTPRIO1 AS CurrencyExchangePortType);
country1 := LabeledEdit1.Text;
country2 := LabeledEdit2.Text;
// i think something wrong with out parameter exRate????
myExchangeRate.getRate(country1, country2, exRate);
LabeledEdit3.Text := FloatToStr(exRate);
end;

What is the problem in calling procedure.
Do have code for other demos at xmethods.com? I want to give a try?
Thanx for help!


Respond

RE: Other Demo Webservices at xmethods.com
S S B Magesh Puvananthiran (Sep 6 2002 2:46AM)

This code should work fine.
I think you might have not set either the Service or Port property of the HTTPRIO component. You will get the error "The parameter is incorrect" when you have not set either of these properties. Check that and let me know...

I mentioned this in the article clearly:

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.


Good Luck!!!
Magesh.
Respond

RE: RE: Other Demo Webservices at xmethods.com
Feroz Ahanger (Sep 6 2002 7:17AM)

thanks magesh!
actually, i thought i have to use 9090 port no in HTTPRIO. as it was written in the xmethods.net comments section of currency exchange. but since HTTPRIO can get it automatically so it works fine.
now, i am using delphi 7 ent. ver. previously i was using delphi 6 ent. trial ver. if i compile same source code in delphi 6 trial ver. then i get error "Proxy Error (502)" but in delphi 7 it works fine:). in delphi 7, HTTPRIO has extra property "Invoke Options". i guess that plays role.
i want to try writing SOAP servers (a little advance ones--  for eg, customer wants to check whether his supplier "ABC" has all the products (111,222,333) available, their prices etc. basically sendding a bunch of data which a client application requests). i want to write both server side as well as client side applications (web services). may be you can give me some help in this.

thanks.

btw: i tried many webservices from xmethods.net (a little advanced one was the sms from oracle. incase anyone here needs source code. plz let me know:).
Respond

This is not WebSnap
    Boris Yankov (Dec 12 2001 3:29PM)

Why is this article in the category WebSnap?
WebService is another technology totaly different from WebSnap!!!
Respond

RE: This is not WebSnap
S S B Magesh Puvananthiran (Dec 12 2001 5:46PM)

Yes. I know that this is not a web snap article in Borland Terms. But I hope it's something to do with the web applications written in Delphi. That's what I am thinking. Because when I posted that article, I put under the category Internet/Web ; but after sometimes a new category called Web Snap was created and my article has been shifted to that by the web master, I guess. So if you really think it's wrong or wish to have a different category name for this type of articles, please write to the editorial or web master.

Thanks.
Magesh.
Respond

Requires Enterprise
    William Sorensen (Aug 20 2001 7:01PM)

What really burns me about all this is that it's ONLY available in the Enterprise Edition of Delphi 6.  Even client-side access to Web Services isn't in the Pro Edition.  And it's not available separately.

Some of us are professional, client-side developers who need XML and SOAP features, but not Oracle data access and middleware.  Borland seems to have forgotten about us.

Of course, they spent half the BorCon demos showing off these wizards (not to mention several articles on the Community site) without mentioning that they're only in the Enterprise edition.
Respond

RE: Requires Enterprise
S S B Magesh Puvananthiran (Aug 20 2001 9:16PM)

You are right. As you said, most of the features are available with Enterprise edition. Some features can be made available as a separate patch for editions other than Enterprise. Probably, we people, can send a request as a whole to Borland to consider this.

Thanks.
Magesh.
Respond

RE: RE: Requires Enterprise
William Sorensen (Aug 20 2001 10:28PM)

Thanks!

Incidentally, I didn't mean to attack your article in any way.  I appreciate the information.  This is an issue I have with Borland.
Respond

internet error 500
    chp (Aug 15 2001 4:35AM)

I do as you say ,but a exception is thrown out.Tthe error indecate like this:
'internet error 500,the process stopped.....',the httprio's prpperties were set.
Respond

RE: internet error 500
S S B Magesh Puvananthiran (Aug 15 2001 11:42PM)

That is a valid HTTP error code. Have you got this sort of error when you browse the internet? It's nothing but an internal server error. What does this mean to the user? Here it is:

You sent a request to find the book price through the web service and the service finds that the server having this service is busy or having some problem; so it's throwing that error to the user.

What to do next?
The solution for this is to try again at a later point of time. You will definitely get the answer.

Visit this link to find the valid HTTP error codes:
HTTP Error Codes

Good Luck...

Thanks.
Magesh.
Respond

RE: internet error 500
hugang (Aug 20 2001 4:00AM)

  I have the same question as you. and i don't know how to make it run.
it's ok to build a server side servivce with delphi (*.exe) and build  a testing client to us the service that provided.
    but when i make a com object which was build by vb or delphi  and use MS Soap toolkit Generator to generate a wsdl file as service then place this wsdl file in my local web virtual directory  such as :  http:// /owen/webpub/DocSample2.wsdl
     when i build the client appliocation , i used webserivce importor to import this myObject.wsdl to make a included unit:UDocSample2_impl.pas
then drop a THTTPPRIO component on my forms and set the wsdllocation ,port and service ,
   add the follow code to the button click event and trigger it
  procedure Button1Click(Sender:TObject)
   var
     sRetVal:WideString;
begin
     sRetVal:= (HTTPRIO1 as IDocSample1).ShowInfo('aaaaaaa');
      showMessage(sRetVal);
end;

when click the button it always raise the exception saing:Internal server 500
    what's wrong here? is it make a web-service that way?

   i study the wsdl file with produced by ws-soad-toolkit found that nothing uncommon there :

    
      
    

  



   where is the problem occurs from? and how to set the internet serevice?
   Can Delphi use only a wsdl file as service and just use importor to import it into delphi project?  
     when use a raw wsdl file as web service ,what's the listener of the wsdl file?
      anyone can give a example that use raw wsdl file as web-service that run at localmachine ?


  




Respond

Invalid Handle
    Tom Peiffer (Aug 13 2001 11:59PM)

I doing exactly what you say in your article, but I get an "Invalid Handle Error" as a result when I try to push the button1. What am I doing wrong?
Respond

RE: Invalid Handle
S S B Magesh Puvananthiran (Aug 14 2001 12:37AM)

I suspect whether you read the following lines in the article or not.

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.


Yes. After dropping the THTTPRIO component into the form, you have to set the WSDLLocation, Service and Port properties in the Object Inspector.  

Set these properties first and try again.

Good Luck!!!

Magesh.
Respond

RE: RE: Invalid Handle
Tom Peiffer (Aug 14 2001 10:41AM)

Thanks, it works fine now. I really forgot to set the port property.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
L. Rosenstein
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)