Mega Code Archive

 
Categories / Delphi / Examples
 

Client-server with web services

Web Service applications are a special form of Web Server application. Support for Web Services is built on top of the Web Broker architecture. To understand the code that the SOAP Application wizard generates, therefore, it is helpful to understand the Web Broker architecture. You will need D6 or 7 Enterprise and a webserver for this project. The Server Start with launching the SOAP application wizard, choose File|New|Other, and on the WebServices page, double-click the Soap Server Application icon. Choose the type of Web server application you want to use for your Web Service. In our case it will be: CGI stand-alone executable. In the "Add New WebService" - dialog, write: The wizard generates a new Web server application that includes a Web module which contains three components: An invoker component (THTTPSoapPascalInvoker). The invoker converts between SOAP messages and the methods of any registered invokable interfaces in your Web Service application. A dispatcher component (THTTPSoapDispatcher). The dispatcher automatically responds to incoming SOAP messages and forwards them to the invoker. You can use its WebDispatch property to identify the HTTP request messages to which your application responds. This involves setting the PathInfo property to indicate the path portion of any URL directed to your application, and the MethodType property to indicate the method header for request messages. A WSDL publisher (TWSDLHTMLPublish). The WSDL publisher publishes a WSDL document that describes your interfaces and how to call them. The WSDL document tells clients how to call on your Web Service application. The SOAP dispatcher and WSDL publisher are auto-dispatching components. This means they automatically register themselves with the Web module so that it forwards any incoming requests addressed using the path information they specify in their WebDispatch properties. If you right-click on the Web module, you can see that in addition to these auto-dispatching components, it has a single Web action item named DefaultHandler. DefaultHandler is the default action item. That is, if the Web module receives a request for which it can't find a handler (can't match the path information), it forwards that message to the default action item. DefaultHandler generates a Web page that describes your Web Service. To change the default action, edit this action item's OnAction event handler. Let's start some coding. We'll just make a tiny interface to see if things work. By now you should have three units in your project: Unit1, SOAPAdderImpl and SOAPAdderIntf. Place the following code in the implementation unit: { Invokable implementation File for TSOAPAdder which implements ISOAPAdder } unit SOAPAdderImpl; interface uses InvokeRegistry, Types, XSBuiltIns, SOAPAdderIntf; type { TSOAPAdder } TSOAPAdder = class(TInvokableClass, ISOAPAdder) public function Add(A, B: integer): integer; stdcall; end; implementation { TSOAPAdder } function TSOAPAdder.Add(A, B: integer): integer; begin Result:= A + B; end; initialization { Invokable classes must be registered } InvRegistry.RegisterInvokableClass(TSOAPAdder); end. Place the Add-function in the interface unit as well: { Invokable interface ISOAPAdder } unit SOAPAdderIntf; interface uses InvokeRegistry, Types, XSBuiltIns; type { Invokable interfaces must derive from IInvokable } ISOAPAdder = interface(IInvokable) ['{66FAF3AA-0228-47D2-A49A-250D86DE2C98}'] function Add(A, B: integer): integer; stdcall; { Methods of Invokable interface must not use the default } { calling convention; stdcall is recommended } end; implementation initialization { Invokable interfaces must be registered } InvRegistry.RegisterInterface(TypeInfo(ISOAPAdder)); end. Save your project as: SOAPAdder_cgi.dpr and compile it. You should now have a file called: SOAPAdder_cgi.exe in your project folder. Copy this file to your webserver's library /cgi-bin You should be able to see a description of your interface at: http://<your server>/cgi-bin/SOAPAdder_cgi.exe This completes the server part of our lesson. Let's go on with the client. The Client First we must get the interface description from the webserver. In your webbrowser: Click on the ISOAPAdder WSDL-link and copy the path to the wsdl file. (http://<your server>/cgi-bin/SOAPAdder_cgi.exe/wsdl/ISOAPAdder) Choose File|New|Other, and on the WebServices page, double-click on the WSDL-importer icon. Paste the path from above into the EditBox and click Next and Finish. You now got a Pascal description file of your interface. Save the file as: ISOAPAdder1.pas. Place it in the same directory as where you wan't the client files. Start a new application. Put three EditBoxes, a label, a button and a HTTPRIO component on the form: Paste the wsdl-path from above into the HTTPPRIO.WSDLLocation property. Add ISOAPAdder1 to the uses clause, and save the project. Put this code in Unit1: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ISOAPAdder1, InvokeRegistry, Rio, SOAPHTTPClient, StdCtrls; type TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Button1: TButton; Label1: TLabel; HTTPRIO1: THTTPRIO; procedure Button1Click(Sender: TObject); private function GetInterface: ISOAPAdder; { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} { TForm1 } function TForm1.GetInterface: ISOAPAdder; begin Result:= HTTPRIO1 as ISOAPAdder; end; procedure TForm1.Button1Click(Sender: TObject); begin Edit3.Text:= IntToStr(GetInterface.Add(StrToInt(Edit1.Text),StrToInt(Edit2.Text))); end; end. Execute and test your client.