delphi xe10.2.3 开发WEBServer 实例

今天2019年01月21日,因工作需要,需要开发WEBServer 接口对接,一直没有写过,感觉好陌生,终于在网上找到资料,把例子做完,并编译成功。感谢疯狂的DELPHI网站。

服务端:

相关资料:

http://www.2ccc.com/news/Html/?1507.html

http://www.dfwlt.com/forum.php?mod=viewthread&tid=922

DelphiXE10.2.3新建WebService具体操作
1.打开“DelphiXE7”->“File”->“New”->“Other”
2.“New Items”->“Delphi Projects”->“WebSrvice”->“SOAP Server Application”
3.“Stand-alone application”->“Next”
4.“VCL application”->“Next”
5.“8080”->“Finish”
6.“Create Interface for SOAPmodule?”->“Yes”
7.“Add New WebService”->输入服务名字“MyData”->“OK”
8.保存全部工程文件
9.在“WebModuleUnit1”单元中放入控件:
FDConnection1
FDPhysMSSQLDriverLink1
FDQuery1
DataSetProvider1
ClientDataSet1
10.双击FDConnection1->Definition->Driver ID:->“MSAcc”->Daabase->“E:\MyData.mdb”->LoginPrompt:=False->Connected:=True
11.FDQuery1->Connection:=FDConnection1->SQL:=“select * from usesr”->Active:=True
12.DataSetProvider1->DataSet:=FDQuery1
13.ClientDataSet1->ProvideName:=DataSetProvider1

代码:

服务端-实例代码: 

复制代码

 1 unit WebModuleUnit1;
 2 
 3 interface
 4 
 5 uses System.SysUtils, System.Classes, Web.HTTPApp, Soap.InvokeRegistry,
 6   Soap.WSDLIntf, System.TypInfo, Soap.WebServExp, Soap.WSDLBind, Xml.XMLSchema,
 7   Soap.WSDLPub, Soap.SOAPPasInv, Soap.SOAPHTTPPasInv, Soap.SOAPHTTPDisp,
 8   Soap.WebBrokerSOAP, FireDAC.Stan.Intf, FireDAC.Stan.Option,
 9   FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
10   FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.MSAcc,
11   FireDAC.Phys.MSAccDef, FireDAC.Phys.MSSQLDef, FireDAC.Stan.Param,
12   FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, Datasnap.DBClient,
13   Datasnap.Provider, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client,
14   FireDAC.Phys.ODBCBase, FireDAC.Phys.MSSQL;
15 
16 type
17   TWebModule1 = class(TWebModule)
18     HTTPSoapDispatcher1: THTTPSoapDispatcher;
19     HTTPSoapPascalInvoker1: THTTPSoapPascalInvoker;
20     WSDLHTMLPublish1: TWSDLHTMLPublish;
21     FDConnection1: TFDConnection;
22     FDPhysMSSQLDriverLink1: TFDPhysMSSQLDriverLink;
23     FDQuery1: TFDQuery;
24     DataSetProvider1: TDataSetProvider;
25     ClientDataSet1: TClientDataSet;
26     procedure WebModule1DefaultHandlerAction(Sender: TObject;
27       Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
28   private
29     { Private declarations }
30   public
31   function GetInfo: widestring;
32   function SetSQL(ASQL: widestring): widestring;
33     { Public declarations }
34   end;
35 
36 var
37   WebModuleClass: TComponentClass = TWebModule1;
38 
39 implementation
40 
41 {%CLASSGROUP 'Vcl.Controls.TControl'}
42 
43 {$R *.dfm}
44 
45 function TWebModule1.GetInfo: widestring;
46 begin
47   ClientDataSet1.Close;
48   ClientDataSet1.Open;
49   Result := ClientDataSet1.XMLData;
50   ClientDataSet1.Close;
51 end;
52 
53 function TWebModule1.SetSQL(ASQL: widestring): widestring;
54 begin
55   FDQuery1.Close;
56   FDQuery1.SQL.Text := ASQL;
57   try
58     FDQuery1.ExecSQL;
59     Result := '成功 ';
60   except
61     Result := '失败';
62   end;
63 end;
64 
65 procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
66   Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
67 begin
68   WSDLHTMLPublish1.ServiceInfo(Sender, Request, Response, Handled);
69 end;
70 
71 end.

//============================================

 1 { Invokable interface IMyData }
 2 
 3 unit MyDataIntf;
 4 
 5 interface
 6 
 7 uses Soap.InvokeRegistry, System.Types, Soap.XSBuiltIns;
 8 
 9 type
10 
11   { Invokable interfaces must derive from IInvokable }
12   IMyData = interface(IInvokable)
13   ['{865DBF5C-8DE1-4D01-AE04-16D04A3F5EF0}']
14     function GetInfo:widestring;stdcall;
15     function SetSQL(ASQL: widestring): widestring;stdcall;
16     { Methods of Invokable interface must not use the default }
17     { calling convention; stdcall is recommended }
18   end;
19 
20 implementation
21 
22 initialization
23   { Invokable interfaces must be registered }
24   InvRegistry.RegisterInterface(TypeInfo(IMyData));
25 
26 end.

//===============================================================

 1 { Invokable implementation File for TMyData which implements IMyData }
 2 
 3 unit MyDataImpl;
 4 
 5 interface
 6 
 7 uses Soap.InvokeRegistry, System.Types, Soap.XSBuiltIns, MyDataIntf;
 8 
 9 type
10 
11   { TMyData }
12   TMyData = class(TInvokableClass, IMyData)
13   public
14     function GetInfo:widestring;stdcall;
15     function SetSQL(ASQL: widestring): widestring;stdcall;
16   end;
17 
18 implementation
19 uses WebModuleUnit1;
20 
21 { TMyData }
22 
23 function TMyData.GetInfo: widestring;
24 var
25   oDM: TWebModule1;
26 begin
27   oDM := TWebModule1.Create(nil);
28   result := oDM.GetInfo;
29   oDM.Free;
30 end;
31 
32 function TMyData.SetSQL(ASQL: widestring): widestring;
33 var
34   oDM: TWebModule1;
35 begin
36   oDM := TWebModule1.Create(nil);
37   result := oDM.SetSQL(ASQL);
38   oDM.Free;
39 end;
40 
41 initialization
42 { Invokable classes must be registered }
43    InvRegistry.RegisterInvokableClass(TMyData);
44 end.

下面是客户端:Componnet->import WSDL  位置发生了变化

注意:delphi xe10.2.3 开发WEBServer 实例_第1张图片

elphiXE10.2.3客户端具体操作:
1.打开“DelphiXE7”->“File”->“New”->“Other”
2.“New Items”->“Delphi Projects”->“WebSrvice”->“WSDL Importer”
3.“Import WSDL”->WSDL Source中输入“http://localhost:8080/wsdl/IMyData”->“Next”
4.“Automatic SOAP versioning.(Recommended)”->“Next”
5.默认选项->“Finish”
6.Delphi会自动生成IMyData文件->保存
7.放入控件
ClientDataSet1
DataSource1
DBGrid1
8.DataSource1->DataSet:=ClientDataSet1
9.DBGrid1->DataSource:=DataSource1

客户端-实例代码:

 1 unit Unit1;
 2 
 3 interface
 4 
 5 uses
 6   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
 7   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.DBGrids, Data.DB,
 8   Datasnap.DBClient, Vcl.StdCtrls;
 9 
10 type
11   TForm1 = class(TForm)
12     Button1: TButton;
13     ClientDataSet1: TClientDataSet;
14     DataSource1: TDataSource;
15     DBGrid1: TDBGrid;
16     Button2: TButton;
17     Edit1: TEdit;
18     procedure Button1Click(Sender: TObject);
19     procedure Button2Click(Sender: TObject);
20   private
21     { Private declarations }
22   public
23     { Public declarations }
24   end;
25 
26 var
27   Form1: TForm1;
28 
29 implementation
30 uses IMyData1;
31 {$R *.dfm}
32 
33 procedure TForm1.Button1Click(Sender: TObject);
34  var
35   ows: IMyData;
36   s: string;
37 begin
38   ows := GetIMyData(true,'http://localhost:8080/wsdl/IMyData',nil);   //参数中可以使用配置的url
39   s := ows.GetInfo;
40   if length(s) <> 0 then
41   ClientDataSet1.xmldata := s;
42 end;
43 
44 procedure TForm1.Button2Click(Sender: TObject);
45  var
46   ows:IMyData;
47   s:string;
48 begin
49   ows := GetIMyData(true,'http://localhost:8080/wsdl/IMyData',nil);   //参数中可以使用配置的url
50   s := ows.SetSQL('delete from usesr where yonghu=' + QuotedStr('ni2'));
51   if length(s) <> 0 then
52   Edit1.Text := s;
53 end;
54 
55 end.

 

 

你可能感兴趣的:(delphi)