標籤:
原來做過webservice,後來忘記了,今天又學了一下,做個筆記,以供以後瀏覽。
WebService是以服務端給使用者端提供介面來調用的。用Delphi開發WebService時有以下幾個步驟:
以下為DelphiXE3開發的WebService,服務端提供目錄下的檔案讓用戶端查詢,在用戶端會顯示所選擇的檔案,以影像檔為例
一、服務端
1.在菜單"File"->"New"->"Other"->"WebService"中點選,如
2.建立服務端模型,我們先以第一種為例,這樣可以調試,以後可以改成其它格式
3.建立對外介面模板(Soaptest)
4.在介面和實現介面的類中加入以下函數並實現它(GetFileNames是列出所有目錄下的png檔案,GetFile是把要顯示的檔案傳遞給用戶端)
介面實現的代碼:
{ Invokable implementation File for TSoapTest which implements ISoapTest }unit uSoapTestImpl;interfaceuses Soap.InvokeRegistry, System.Types, Soap.XSBuiltIns, uSoapTestIntf,SysUtils,Vcl.Forms,System.Classes,System.IOUtils ;type { TSoapTest } TSoapTest = class(TInvokableClass, ISoapTest) private function GetServerPath:string; public function GetFileNames(out icount:Integer):TStringDynArray;stdcall; function GetFile(const AFile:string; out isize:integer):TByteDynArray;stdcall; end;implementation{ TSoapTest }function TSoapTest.GetFile(const AFile: string; out isize: integer): TByteDynArray;var mem:TMemoryStream; AtmpFile:string;begin isize:=0; SetLength(Result,isize); AtmpFile:=GetServerPath+AFile; if not FileExists(AtmpFile) then raise Exception.Create(‘Cou‘‘t find the AFile‘); mem:=TMemoryStream.Create; try mem.LoadFromFile(AtmpFile); isize:=mem.Size; SetLength(Result,isize); Move(mem.Memory^,result[0],isize); finally mem.Free; end;end;function TSoapTest.GetFileNames(out icount: Integer): TStringDynArray;var i:integer;begin Result:= TDirectory.GetFiles(GetServerPath,‘*.png‘,TSearchOption.soTopDirectoryOnly); icount:=Length(Result); for i := 0 to icount-1 do Result[i]:=ExtractFileName(Result[i]);end;function TSoapTest.GetServerPath: string;begin result:=‘..\imgs\‘;end;initialization{ Invokable classes must be registered } InvRegistry.RegisterInvokableClass(TSoapTest);end.
5.運行服務端,如,開啟瀏覽器後,得到wsdl的檔案
點擊ISoapTest[WSDL]串連得到如片
6.儲存上面的網頁並命名為wsdl格式的檔案,用於用戶端調用
二、開發用戶端
1.建立普通的應用程式,並在介面上放上HTTPRIO組件,用於調用服務端介面函數。如:listbox顯示用戶端的檔案名稱,右邊為image用於顯示點選 的圖片
2.在菜單“Component->Import WSDL”開啟一介面,在location中選擇你剛才儲存的wsdl檔案,用於產生介面單元(記著一定要開啟服務端),並儲存到用戶端,讓用戶端引用
3.在用戶端中寫入以下代碼即可
unit UMain;interfaceuses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,Vcl.Imaging.pngimage, Vcl.ExtCtrls, Vcl.StdCtrls, Soap.InvokeRegistry, Soap.Rio, Soap.SOAPHTTPClient,SoapTest;type TForm2 = class(TForm) ListBox1: TListBox; Image1: TImage; HTTPRIO1: THTTPRIO; Button1: TButton; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure ListBox1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form2: TForm2;implementation uses System.Types;{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);var FilesArr:TStringDynArray; i:integer; str:string;begin ListBox1.Clear; FilesArr:=(HTTPRIO1 as isoaptest).GetFileNames(i); Caption:=‘The total Files is :‘+inttostr(i); for str in FilesArr do ListBox1.Items.Add(str);end;procedure TForm2.FormCreate(Sender: TObject);begin// with HTTPRIO1 do// begin// WSDLLocation:=‘SoapTest.wsdl‘ ;// Service:=‘ISoapTestservice‘;// Port:=‘ISoapTestPort‘;// end; HTTPRIO1.URL:=‘http://localhost:8080/soap/‘;end;procedure Showimg(const i:integer);var mem:TMemoryStream; bytearr:TByteDynArray; AFile:string; isize:integer;begin AFile:=Form2.ListBox1.Items.Strings[i]; bytearr:=(Form2.HTTPRIO1 as ISoapTest).GetFile(AFile,isize); mem:=TMemoryStream.Create; try mem.SetSize(isize); Move(bytearr[0],mem.Memory^,isize); mem.SaveToFile(Afile); Form2.Image1.Picture.LoadFromFile(AFile); finally mem.Free; end;end;procedure TForm2.ListBox1Click(Sender: TObject);var i:integer;begin i:=ListBox1.ItemIndex; Showimg(i);end;end.
說明:
在開發webserver時,盡量減少網路流量,最好不要在HTTPRIO的WSDLLocation屬性中直接輸入服務端的網址,否則會產生get和post的兩次網路迴路。
把網頁儲存下來的wsdl檔案放在用戶端讓HTTPRIO來調用即可,如下面的代碼,Service和Port可以查看在用戶端產生的單元檔案,區分大小寫。這樣可以減少一次網路迴路,調用其它語言寫的webservice就可以這樣。
with HTTPRIO1 do begin WSDLLocation:=‘SoapTest.wsdl‘ ; Service:=‘ISoapTestservice‘; Port:=‘ISoapTestPort‘; end;
另外如果是Delphi寫的WebService,則最好把HTTPRIO的 URL寫進去,URL和WSDLLocation是互斥的,URL調用時soap已做最佳化了,所以相對於WSDL,對網路的壓力要小些,如果是用URL的話, 則不用寫入Service和Port屬性。URL的最後可以不帶介面名稱
WSDL的格式為:http(s)://主機名稱:連接埠/啟動並執行程式(虛擬目錄名)/wsdl/介面名稱, 如樣本的WSDL: http://localhot:8080/wsdl/Isoaptest
URL的格式為:http(s)://主機名稱:連接埠/啟動並執行程式(虛擬目錄名)/Soap/介面名稱,如樣本的URL:http://localhost:8080/Soap/ISoaptest,其實可以把最後的介面名稱消去即可,即http://localhost:8080/soap
如果要處理資料庫方面的,則在服務端可以增加 Soap server data module資料模組,放上要訪問資料庫的組件,如sqlconnection,sqldataset等,在用戶端上放上SoapConnection的串連組件,主要是用來串連服務端的資料群組件的。soapconnection的url可在用戶端產生的介面單元中找到,也可以省去最後的介面名稱。
如有不正確敬請指正。
WebService的一些設計