13.13.2  Delphi Web Services样例程序
  1.服务端
  为了使读者朋友对Web Services程序的开发过程有一个较清晰的认识,这里作者用Delphi给大家做一个简单样例程序。服务端用来提供对外服务接口,只有服务端运行后,其提供的服务接口才能被其他应用所调用,这里我们把调用其服务接口的程序统一叫客户端。
  首先,选择“SOAP Server Application”选项,如图13-110所示。
  单击【OK】按钮,则弹出图13-111所示对话框信息,我们选择 “ISAPI/NSAPI Dynamic Link Library”,单击【OK】按钮,弹出确认对话框,如图13-112所示,单击【Yes】按钮。
     
  
  图13-110  New Items对话框          图13-111  New SOAP Server Application对话框         图13-112  Confirm对话框
  将出现图13-113所示界面信息,您可以在对话框中输入服务名称,这里我们将该服务接口定义为“MyHello”,单击【OK】按钮,将产生相关的单元(Unit)文件,下面将附上相关文件的源代码供大家参考。
              
            图13-113  Confirm对话框                          图13-114  WebModule1对话框(对应单元文件为main.pas)
  main.pas源代码:
{ SOAP WebModule } unit main; interface uses SysUtils, Classes, HTTPApp, InvokeRegistry, WSDLIntf, TypInfo, WebServExp, WSDLBind, XMLSchema, WSDLPub, SOAPPasInv, SOAPHTTPPasInv, SOAPHTTPDisp, WebBrokerSOAP; type TWebModule1 = class(TWebModule) HTTPSoapDispatcher1: THTTPSoapDispatcher; HTTPSoapPascalInvoker1: THTTPSoapPascalInvoker; WSDLHTMLPublish1: TWSDLHTMLPublish; procedure WebModule1DefaultHandlerAction(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean); private { Private declarations } public { Public declarations } end; var WebModule1: TWebModule1; implementation {$R *.dfm} procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean); begin WSDLHTMLPublish1.ServiceInfo(Sender, Request, Response, Handled); end; end. | 
MyHelloImpl.pas源代码:
unit MyHelloImpl; interface uses InvokeRegistry, Types, XSBuiltIns, MyHelloIntf; type   { TMyHello }   TMyHello = class(TInvokableClass, IMyHello)   public     function Welcome(name: string): string; stdcall;   end; implementation  function TMyHello.Welcome(name: string): string; begin   result := '欢迎' + name + '同学!' ; end;
 initialization   { Invokable classes must be registered }   InvRegistry.RegisterInvokableClass(TMyHello); end.  | 
MyHelloIntf.pas源代码:
unit MyHelloIntf; interface uses InvokeRegistry, Types, XSBuiltIns; type   TEnumTest = (etNone, etAFew, etSome, etAlot);   TDoubleArray = array of Double;   TMyEmployee = class(TRemotable)   private     FLastName: AnsiString;     FFirstName: AnsiString;     FSalary: Double;   published     property LastName: AnsiString read FLastName write FLastName;     property FirstName: AnsiString read FFirstName write FFirstName;     property Salary: Double read FSalary write FSalary;   end;   { Invokable interfaces must derive from IInvokable }   IMyHello = interface(IInvokable)     ['{F80D3129-3B13-49A7-8CCF-3DC3B120BA15}']     { Methods of Invokable interface must not use the default }     { calling convention; stdcall is recommended }     function Welcome(name: string): string; stdcall;   end; implementation initialization   { Invokable interfaces must be registered }   InvRegistry.RegisterInterface(TypeInfo(IMyHello)); end.  | 
  接下来,您需要创建一个标准的“Application”,界面信息如图13-115所示。

  图13-115  样例演示-服务端(对应单元文件为u_main.pas)
  u_main.pas源代码:
unit u_main; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, SUIButton, StdCtrls, ExtCtrls, SUIForm, IdHTTPWebBrokerBridge; type TForm1 = class(TForm) sfrm1: TsuiForm; lbl1: TLabel; btn1: TsuiButton; procedure btn1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure sfrm1Click(Sender: TObject); private { Private declarations } ser: TIdHTTPWebBrokerBridge; public { Public declarations } end; var Form1: TForm1; implementation uses main, MyHelloImpl, MyHelloIntf; {$R *.dfm} procedure TForm1.btn1Click(Sender: TObject); begin close; end; procedure TForm1.FormCreate(Sender: TObject); begin ser:=TIdHTTPWebBrokerBridge.Create(self); ser.DefaultPort:=5678; ser.Active:=true; ser.RegisterWebModuleClass(TWebModule1); end; end. | 
  Server.dpr源代码:
program Server; uses Forms, u_main in 'u_main.pas' {Form1}, main in 'main.pas' {WebModule1: TWebModule}, MyHelloImpl in 'MyHelloImpl.pas', MyHelloIntf in 'MyHelloIntf.pas'; {$R *.res} begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.CreateForm(TWebModule1, WebModule1); Application.Run; end. | 
  所有源代码编写完成后,单击“F9”运行程序,将弹出图13-116所示界面。
  如图13-116所示,“样例演示-服务端”小程序运行后,您可以打开IE,输入“http://localhost:5678/”来检验先前完成的服务是否可以成功展示,如图13-117所示。
     
   
  图13-116 “样例演示-服务端”小程序                      图13-117  服务接口相关信息         
    
13-118  样例演示-客户端 
   2.客户端
  最后,让我们来制作一个客户端小程序来调用先前完成的接口。
  创建一个标准的Delphi应用,其界面设计如图13-118所示。
  应用“WSDL Import Wizard”工具引入接口,如图13-119和图13-120所示。
       
  图13-119 “New Items-WSDL Importer”对话框                   图13-120 “WSDL Import Wizard”对话框
  引入服务接口后,将生成“IMyHello1.pas”单元文件,其源代码如下:
// ************************************************************************ // // The types declared in this file were generated from data read from the // WSDL File described below: // WSDL     : http://localhost:5678/wsdl/IMyHello // Encoding : utf-8 // Version  : 1.0 // (2012-11-11 下午 02:02:42 - 1.33.2.5) // ************************************************************************ // unit IMyHello1; 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 // ************************************************************************ // // Namespace : urn:MyHelloIntf-IMyHello // soapAction: urn:MyHelloIntf-IMyHello#Welcome // transport : http://schemas.xmlsoap.org/soap/http // style     : rpc // binding   : IMyHellobinding // service   : IMyHelloservice // port      : IMyHelloPort // URL       : http://localhost:5678/soap/IMyHello< // ************************************************************************ // IMyHello = interface(IInvokable) ['{FEDC3D83-ACE9-0403-6D1D-C1B54AA0B54C}'] function Welcome(const name: WideString): WideString; stdcall; end; function GetIMyHello(UseWSDL: Boolean = System.False; Addr: string = ''; HTTPRIO: THTTPRIO = nil): IMyHello; implementation function GetIMyHello(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): IMyHello; const defWSDL = 'http://localhost:5678/wsdl/IMyHello'; defURL = 'http://localhost:5678/soap/IMyHello'; defSvc = 'IMyHelloservice'; defPrt = 'IMyHelloPort'; 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 IMyHello); 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; initialization InvRegistry.RegisterInterface(TypeInfo(IMyHello), 'urn:MyHelloIntf-IMyHello', 'utf-8'); InvRegistry.RegisterDefaultSOAPAction(TypeInfo(IMyHello), 'urn:MyHelloIntf-IMyHello#Welcome'); end. | 
  .Unit1.pas源代码:
unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs, SUIButton, ExtCtrls, SUIForm, StdCtrls, SUIEdit; type   TForm1 = class(TForm)     sfrm1: TsuiForm;     btn1: TsuiButton;     lbl1: TLabel;     edt1: TsuiEdit;     btn2: TsuiButton;     lbl2: TLabel;     lbl3: TLabel;     procedure btn1Click(Sender: TObject);     procedure btn2Click(Sender: TObject);   private     { Private declarations }   public     { Public declarations }   end; var   Form1: TForm1; implementation uses IMyHello1; {$R *.dfm} procedure TForm1.btn1Click(Sender: TObject); var   I: IMyHello; begin   I := GetIMyHello;   if Trim(edt1.Text) <> '' then begin     lbl2.Caption := I.Welcome(edt1.Text);     I := nil;   end   else begin     Application.MessageBox('请输入姓名!', '系统信息', 0);     Exit;   end; end; procedure TForm1.btn2Click(Sender: TObject); begin   Close; end; end.  | 
  在Delphi IDE环境,单击“F9”运行客户端程序,将弹出图13-121所示对话框。
后续内容请从书籍获得……
  (未完待续)
版权声明:51Testing软件测试网及相关内容提供者拥有51testing.com内容的全部版权,未经明确的书面许可,任何人或单位不得对本网站内容复制、转载或进行镜像。51testing软件测试网欢迎与业内同行进行有益的合作和交流,如果有任何有关内容方面的合作事宜,请联系我们。
相关链接:
精通软件性能测试与LoadRunner最佳实战 连载十二