Web Services Made Simple in Delphi 6...


How do we access a web service from Delphi?
How do we use SOAP components in Delphi?

You can learn the basics of computer programming and creating software of your own by checking out the excellent Software Requirements Unleashed. To learn how to access web services using SOAP, read on!

Accessing web services using SOAP...

In Delphi 6, accessing web services using SOAP is made very easy with a complete set of components in the WebServices palette. In this article, I'm going to present a simple example in Delphi 6 on how to use 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 Fahrenheit. 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 at http://www.xmethods.net. There are a lot of such web services available on that site.

How it works?

For example, if you want to convert a temperature from Centigrade to Fahrenheit, then you will probably input the Centigrade temperature to that service. Now that input is prepared as an XML request and sent to the web service. Then the web service performs the conversion and sends 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 of its functionality.

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 Delphi 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; a 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 WebServices palette in Delphi 6. This component will help us invoke the method of the web service. Create a new application and drop that component.

a. Set the WSDLLocation, Service, and 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.

Enjoy!!!

Thanks.
Magesh.








Web Services Made Simple in Delphi 6...


Web Services Made Simple in Delphi 6... : Multi-thousand tips to make your life easier.


How do we access a web service from Delphi?
How do we use SOAP components in Delphi?

You can learn the basics of computer programming and creating software of your own by checking out the excellent Software Requirements Unleashed. To learn how to access web services using SOAP, read on!

Accessing web services using SOAP...

In Delphi 6, accessing web services using SOAP is made very easy with a complete set of components in the WebServices palette. In this article, I'm going to present a simple example in Delphi 6 on how to use 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 Fahrenheit. 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 at http://www.xmethods.net. There are a lot of such web services available on that site.

How it works?

For example, if you want to convert a temperature from Centigrade to Fahrenheit, then you will probably input the Centigrade temperature to that service. Now that input is prepared as an XML request and sent to the web service. Then the web service performs the conversion and sends 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 of its functionality.

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 Delphi 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; a 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 WebServices palette in Delphi 6. This component will help us invoke the method of the web service. Create a new application and drop that component.

a. Set the WSDLLocation, Service, and 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.

Enjoy!!!

Thanks.
Magesh.

Web Services Made Simple in Delphi 6...

Web Services Made Simple in Delphi 6... : Multi-thousand tips to make your life easier.
Web Services Made Simple in Delphi 6...
Web Services Made Simple in Delphi 6...
Recommend to friends
  • gplus
  • pinterest

Comment

Leave comment

Rating