編寫一個單獨的Web Service for Delphi

早段時間看了一篇好文章,對其整理成如下幾步

1新建一個SOAP Server Application,在提示輸入接口時輸入MyHello,把全部文件
保存在一個叫Ser的目錄下,其中一個包含TWebModule1的文件保存爲main.pas.
在MyHelloIntf.pas的IMyHello接口中加入
function Welcome(name:string):string;stdcall;   

在MyHelloImpl.pas中的TMyHello實現此方法
function TMyHello.Welcome(name:string):string;
begin
  result:='Welcome '+name;
end;

2新建一個標準Application,把全部文件保存在剛纔哪一個Ser目錄下(同一目錄).
  文件名默認,在Unit1.pas中Uese IdHTTPWebBrokerBridge,可能還要在工程
的Search path加入<Delphi7>\Source\Indy  <Delphi7>是Delphi的安裝目錄
3在form1上放一個lable,寫上"Stand Alone Web Service"
4在TForm1的private中加入一個變量ser:TIdHTTPWebBrokerBridge;
5Uses第一步中的main.pas MyHelloIntf.pas MyHelloImpl.pas 
7在OnFormCreate事件上寫
  ser:=TIdHTTPWebBrokerBridge.Create(self);
  ser.DefaultPort:=5678;
  ser.Active:=true;
  ser.RegisterWebModuleClass(TWebModule1);
8運行程序,打開IE,輸入http://localhost:5678/.結果你們都想到了

寫Client
1關閉全部文件.
2新建一個標準Application
3運行剛纔寫的服務器Application
4運行wsdl import wizard,在URL中輸入http://localhost:5678/wsdl/IMyHello
  按Next幾回
5保存全部文件到一個新目錄,用Wizard產生的文件保存爲IMyHello1.pas,其他默認,在Unit1.pas中uses 

IMyHello1.pas,放一個TButton,寫上下面的代碼,運行.
procedure TForm1.Button1Click(Sender: TObject);
var
  I:IMyHello;
begin
  I:=GetIMyHello;
  ShowMessage(I.Welcome('black man')); 
  I:=nil;
end;web

相關文章
相關標籤/搜索