delphi7开发webservice部属在apache服务器中
用Delphi7开发Web Service程序,并把服务程序放在apache Web服务器上提供给各种客户程序调用。
第一步 编写服务程序
第一步:File----->New----->Other------>WebServices----->Soap Server Application
选择CGI Stand-alone executable然后确定,然后输入接口名字。
第二步:定义一个接口单元。先通过向导生成一个空的单元文件,然后在此单元中实现基本的接口(Iinvokable)和定义以后客户端程序可以调用的方法,原代码如下:
接口代码
{ Invokable interface IMyWeb }unit MyWebIntf;interfaceuses InvokeRegistry, Types, XSBuiltIns;;//基本的结构和方法的定义都在此单元中,必须引type { Invokable interfaces must derive from IInvokable } IMyWeb = interface(IInvokable) //自定义的一个结构,继承自Iinvokable ['{E3070F4D-AEBF-47D6-963B-ADFFC4E7C7A1}'] //通过Ctrl+Shift+G生成的一个GUID { Methods of Invokable interface must not use the default } { calling convention; stdcall is recommended } function gettext():widestring;stdcall;//自定义的一个方法,也是以后客户可以调用的方 end;implementationinitialization { Invokable interfaces must be registered } InvRegistry.RegisterInterface(TypeInfo(IMyWeb));//通过此方法来注册接end.
{ Invokable implementation File for TMyWeb which implements IMyWeb }unit MyWebImpl;interfaceuses InvokeRegistry, Types, XSBuiltIns, MyWebIntf,Unit1;//引用自定义的接口单元type { TMyWeb } TMyWeb = class(TInvokableClass, IMyWeb)//定义实现类,此类必须继承自TInvokableClass,并实现自定义接 public function gettext():widestring;stdcall;//申明在自定义接口中所定义的方法 end;implementationfunction TMyWeb.gettext: widestring;//实现自定义方法beginResult:='Success';end;initialization { Invokable classes must be registered } InvRegistry.RegisterInvokableClass(TMyWeb);end.
// ************************************************************************ //// The types declared in this file were generated from data read from the// WSDL File described below:// WSDL : http://127.0.0.1:8090/cgi-bin/Project2.exe/wsdl/IMyWeb// Encoding : utf-8// Version : 1.0// (2011-7-22 16:14:10 - 1.33.2.5)// ************************************************************************ //unit IMyWeb1;interfaceuses 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:MyWebIntf-IMyWeb // soapAction: urn:MyWebIntf-IMyWeb#gettext // transport : http://schemas.xmlsoap.org/soap/http // style : rpc // binding : IMyWebbinding // service : IMyWebservice // port : IMyWebPort // URL : http://127.0.0.1:8090/cgi-bin/Project2.exe/soap/IMyWeb // ************************************************************************ // IMyWeb = interface(IInvokable) ['{85474E46-8BF2-FC4E-8A91-6FC82BB6EBF1}'] function gettext: WideString; stdcall; end;function GetIMyWeb(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): IMyWeb;implementationfunction GetIMyWeb(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): IMyWeb;const defWSDL = 'http://127.0.0.1:8090/cgi-bin/Project2.exe/wsdl/IMyWeb'; defURL = 'http://127.0.0.1:8090/cgi-bin/Project2.exe/soap/IMyWeb'; defSvc = 'IMyWebservice'; defPrt = 'IMyWebPort';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 IMyWeb); 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(IMyWeb), 'urn:MyWebIntf-IMyWeb', 'utf-8'); InvRegistry.RegisterDefaultSOAPAction(TypeInfo(IMyWeb), 'urn:MyWebIntf-IMyWeb#gettext');end.
unit MyWebImpl;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,IMyWeb1, StdCtrls; //IMyWeb1 为引入自动生成的webservice服务器接口type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);vartestobj:IMyWeb;//定义对象begintestobj:=GetIMyWeb;//创建对象showmessage(testobj.gettext);//调用方法end;end.