前幾天做了linux下apache的開發,今天做一個linux 下的webservice ,以供客戶端調用。
閑話少說,直接干。
新建一個工程。選other...,選擇如圖。
繼續輸入服務名
然后就生成對應的單元。
增加linux 平台。
完善對應的單元代碼
{ Invokable implementation File for Txaliontest which implements Ixaliontest } unit xaliontestImpl; interface uses Soap.InvokeRegistry, System.Types, Soap.XSBuiltIns, xaliontestIntf; type { Txaliontest } Txaliontest = class(TInvokableClass, Ixaliontest) public function echoname(const Value:string):string; stdcall; function sumall(const Value:integer):integer; stdcall; end; implementation { Txaliontest } function Txaliontest.echoname(const Value: string): string; begin result:='你好'+value; end; function Txaliontest.sumall(const Value: integer): integer; var i:integer; isumall:integer; begin isumall:=0; for i := 1 to value do isumall:=isumall+i; result:=isumall; end; initialization { Invokable classes must be registered } InvRegistry.RegisterInvokableClass(Txaliontest); end.
{ Invokable interface Ixaliontest } unit xaliontestIntf; interface uses Soap.InvokeRegistry, System.Types, Soap.XSBuiltIns; type { Invokable interfaces must derive from IInvokable } Ixaliontest = interface(IInvokable) ['{20590E4A-BF8C-41AE-A630-94D2DD38EEE1}'] { Methods of Invokable interface must not use the default } { calling convention; stdcall is recommended } function echoname(const Value:string):string; stdcall; function sumall(const Value:integer):integer; stdcall; end; implementation initialization { Invokable interfaces must be registered } InvRegistry.RegisterInterface(TypeInfo(Ixaliontest)); end.
編譯本工程。
哎呀,怎么出錯了?
不要害怕,這是因為linux 上apache的引用沒有加入。我們確認apache 已經在linux上安裝成功。
那么我們在IDE 里面處理一下。
記住設以上的地方為True, 允許未定義的引用。
現在重新編譯
哈哈,OK 了
現在的任務就是在發布這個apache 模塊。
這一塊的內容請參照前面的文章。
配置文件如圖:
啟動apache.
在瀏覽器里面輸入對應的地址,就可以顯示webservice 的接口信息了。
好了,服務端搞定了。我們做一個客戶端來調用一下這個服務。
直接建一個vcl application .
然后選擇WDSL 導入器。
輸入對應的地址。
系統會生成對應的接口文件
// ************************************************************************ // // The types declared in this file were generated from data read from the // WSDL File described below: // WSDL : http://192.168.1.66/xalionws/wsdl/Ixaliontest // Encoding : utf-8 // Version : 1.0 // (2017-4-8 21:33:51 - - $Rev: 90173 $) // ************************************************************************ // unit Ixaliontest1; interface uses Soap.InvokeRegistry, Soap.SOAPHTTPClient, System.Types, Soap.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 Embarcadero types; however, they could also // indicate incorrect WSDL documents that failed to declare or import a schema type. // ************************************************************************ // // !:int - "http://www.w3.org/2001/XMLSchema"[] // !:string - "http://www.w3.org/2001/XMLSchema"[] // ************************************************************************ // // Namespace : urn:xaliontestIntf-Ixaliontest // soapAction: urn:xaliontestIntf-Ixaliontest#%operationName% // transport : http://schemas.xmlsoap.org/soap/http // style : rpc // use : encoded // binding : Ixaliontestbinding // service : Ixaliontestservice // port : IxaliontestPort // URL : http://192.168.1.66/xalionws/soap/Ixaliontest // ************************************************************************ // Ixaliontest = interface(IInvokable) ['{A3FB1A48-1AE7-B449-16DC-42CCE1A48832}'] function echoname(const Value: string): string; stdcall; function sumall(const Value: Integer): Integer; stdcall; end; function GetIxaliontest(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): Ixaliontest; implementation uses System.SysUtils; function GetIxaliontest(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): Ixaliontest; const defWSDL = 'http://192.168.1.66/xalionws/wsdl/Ixaliontest'; defURL = 'http://192.168.1.66/xalionws/soap/Ixaliontest'; defSvc = 'Ixaliontestservice'; defPrt = 'IxaliontestPort'; 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 Ixaliontest); 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 { Ixaliontest } InvRegistry.RegisterInterface(TypeInfo(Ixaliontest), 'urn:xaliontestIntf-Ixaliontest', 'utf-8'); InvRegistry.RegisterDefaultSOAPAction(TypeInfo(Ixaliontest), 'urn:xaliontestIntf-Ixaliontest#%operationName%'); end.
在主窗體放兩個按鈕。
代碼如下
implementation {$R *.dfm} uses Ixaliontest1; procedure TForm2.Button1Click(Sender: TObject); begin showmessage( GetIxaliontest.echoname('xalion')); end; procedure TForm2.Button2Click(Sender: TObject); begin showmessage( GetIxaliontest.sumall(100).tostring); end;
編譯運行。
可以看見,客戶端很順利的調用了服務器端的函數。
我們今天的任務完成了。