Delphi 7 中使用Indy创建独立的 Web Services/SOAP 服务器(一)

创建独立的Web Server应用MySOAPServer.exe。

该程序使用了Delphi 7 企业版自带的 Indy 9中的IdHTTPWebBrokerBridg来创建独立的SOAP Web Service应用,无需IIS,Apache等Web服务器应用。

从 Delphi7\Source\Indy目录拷贝两个文件"IdHTTPWebBrokerBridge.pas" 和"IdCompilerDefines.inc"到你的工程目录。

从菜单File --> New --> Other中选择WebServices标签,选择SOAP Server Application图标, 点OK

新建 SOAP Server应用,选择Web App Debugger executable

在Class Name框中输入: Dummy
弹出确认对话框Create Interface for SOAP Module?,点Yes.
新建WebService,Service Name中输入: MySOAPWebService,Unit Identifier中会自动填入相同值,然后点OK。


View --> Project Manager打开项目管理器,展开Project(n).exe目录

选中MySOAPWebServiceImpl.pas按"DEL"键,弹出确认窗口“Remove MySOAPWebServiceImpl.pas?",点Yes

接下来问你 Save changes to MySOAPWebServiceImpl.pas?,点No(Impl.pas没有必要)


View --> Project Manager打开项目管理器,展开Unit1目录,双击Unit1.pas,删除initialization部分的代码:

 initialization
    TWebAppSockObjectFactory.Create('Dummy');


View --> Project Manager打开项目管理器,双击Form1,按F11激活对象观察器,将Name属性从Form1改为MySOAPServerForm

View --> Project Manager打开项目管理器,展开Unit2目录,双击WebModule2,按F11激活对象观察器,将Name属性从WebModule2改为MyWebModule

View --> Project Manager打开项目管理器,双击Unit2.pas,在initialization部分,对WebRequestHandler.WebModuleClass赋值时检查一下是否为nil

initialization
    if WebRequestHandler <> nil then
      WebRequestHandler.WebModuleClass := TMyWebModule;

File --> New --> Other,选择WebServices标签,选中SOAP Server Data Module图标 点Ok

在Soap Data Module Wizard向导窗口中输入Module Name: MyAppServerSoapDataModule

View --> Project Manager打开项目管理器,展开Unit3目录,双击Unit3.pas,

在 interface部分uses中加入MySOAPWebServiceIntf:

  uses
    SysUtils, Classes, InvokeRegistry, Midas, SOAPMidas, SOAPDm,
    MySOAPWebServiceIntf;

在TMyAppServerSoapDM声明部分加入IMySOAPWebService

    TMyAppServerSoapDataModule = class(TSoapDataModule,
      IMyAppServerSoapDataModule, IAppServerSOAP, IAppServer,
      IMySOAPWebService)

  [注意结尾没有分号]


View --> Project Manager打开项目管理器,双击Unit1.pas

在interface部分uses中加入IdHTTPWebBrokerBridge

  uses
    SysUtils, Classes, Forms, IdHTTPWebBrokerBridge;
   Add the private variable FWebBrokerBridge to the TForm1 declaration.
    private
      FWebBrokerBridge: TIdHTTPWebBrokerBridge;

在 implementation 部分uses中加入Unit2

 uses
    SockApp, Unit2;

View --> Project Manager打开项目管理器,双击MySOAPServerForm,按F11激活对象观察器,选Event标签,双击OnCreate事件,FormCreate方法加入下面代码:

  // Create server.
  FWebBrokerBridge := TIdHTTPWebBrokerBridge.Create(Self);
  // Register web module class.
  FWebBrokerBridge.RegisterWebModuleClass(TMyWebModule);
  // Set default port.
  FWebBrokerBridge.DefaultPort := 1024;
  // Start server.
  FWebBrokerBridge.Active := True;


按F11激活对象观察器,选Event标签,双击OnDestroy事件,FormDestroy方法加入下面代码:

  // Stop server.
  FWebBrokerBridge.Active := False;
  // Free server component.
  FreeAndNil(FWebBrokerBridge);

到此应该可以运行程序,它将侦听1024端口的SOAP请求,在浏览器中打开 http://localhost:1024/wsdl/IMySOAPWebService,会显示XML。



接下来在服务程序中加入SOAP方法。


View --> Project Manager打开项目管理器,双击Unit3.pas,在TMyAppServerSoapDataModule的声明部分加入webservice的publice方法声明

  public
    function HelloWorld(Param1: string): string; stdcall;

方法的实现代码为:

  { TMyAppServerSoapDataModule }
 function TMyAppServerSoapDataModule.HelloWorld(Param1:  string): string; stdcall;
 begin
    Result := 'Hello World.  Your data: ' + Param1;
end;

View --> Project Manager打开项目管理器,双击MySOAPWebServiceIntf.pas,

在IMySOAPWebService中加入webservice方法的声明:

    IMySOAPWebService = interface(IInvokable)
    ['{4BF42BCF-294B-4D28-93D8-8311FA8B190E}']
      function HelloWorld(Param1: string): string; stdcall;
    end;

现在你可以保存文件:

点File --> Save Project As,
   保存Unit1,文件名为MySOAPServerFormU.pas
   保存Unit2,文件名为MyWebModuleU.pas
   保存MySOAPWebServiceIntf,文件名为MySOAPWebServiceIntf.pas
   保存Unit3,文件名为MyAppServerSoapDataModuleU.pas
   保存Project(n) As,文件名为 MySOAPServer.dpr

View --> Project Manager打开项目管理器,展开MySOAPServerFormU目录,双击MyFormU.pas,将Unit2引用改为MyWebModuleU.

  uses
    SockApp,
    MyWebModuleU;

点File --> Save,进行保存。

现在你可以再次运行程序,它仍然将侦听1024端口的SOAP请求。


http://www.digicoast.com/delphi_soap_standalone.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值