精通軟件性能測試與LoadRunner最佳實戰 連載十三
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 } implementation
initialization 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軟件測試網歡迎與業內同行進行有益的合作和交流,如果有任何有關內容方面的合作事宜,請聯系我們。
相關鏈接:
posted on 2013-07-15 10:19 順其自然EVO 閱讀(595) 評論(0) 編輯 收藏 所屬分類: 測試學習專欄 、loadrunner