DelphiXE7中创建WebService(服务端+客户端)

时间:2022-05-07 20:48:16

相关资料:

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

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

DelphiXE7新建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

服务端-实例代码:

 unit WebModuleUnit1;

 interface

 uses System.SysUtils, System.Classes, Web.HTTPApp, Soap.InvokeRegistry,
Soap.WSDLIntf, System.TypInfo, Soap.WebServExp, Soap.WSDLBind, Xml.XMLSchema,
Soap.WSDLPub, Soap.SOAPPasInv, Soap.SOAPHTTPPasInv, Soap.SOAPHTTPDisp,
Soap.WebBrokerSOAP, FireDAC.Stan.Intf, FireDAC.Stan.Option,
FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.MSAcc,
FireDAC.Phys.MSAccDef, FireDAC.Phys.MSSQLDef, FireDAC.Stan.Param,
FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, Datasnap.DBClient,
Datasnap.Provider, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client,
FireDAC.Phys.ODBCBase, FireDAC.Phys.MSSQL; type
TWebModule1 = class(TWebModule)
HTTPSoapDispatcher1: THTTPSoapDispatcher;
HTTPSoapPascalInvoker1: THTTPSoapPascalInvoker;
WSDLHTMLPublish1: TWSDLHTMLPublish;
FDConnection1: TFDConnection;
FDPhysMSSQLDriverLink1: TFDPhysMSSQLDriverLink;
FDQuery1: TFDQuery;
DataSetProvider1: TDataSetProvider;
ClientDataSet1: TClientDataSet;
procedure WebModule1DefaultHandlerAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
private
{ Private declarations }
public
function GetInfo: widestring;
function SetSQL(ASQL: widestring): widestring;
{ Public declarations }
end; var
WebModuleClass: TComponentClass = TWebModule1; implementation {%CLASSGROUP 'Vcl.Controls.TControl'} {$R *.dfm} function TWebModule1.GetInfo: widestring;
begin
ClientDataSet1.Close;
ClientDataSet1.Open;
Result := ClientDataSet1.XMLData;
ClientDataSet1.Close;
end; function TWebModule1.SetSQL(ASQL: widestring): widestring;
begin
FDQuery1.Close;
FDQuery1.SQL.Text := ASQL;
try
FDQuery1.ExecSQL;
Result := '成功 ';
except
Result := '失败';
end;
end; procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
WSDLHTMLPublish1.ServiceInfo(Sender, Request, Response, Handled);
end; end.
 { Invokable interface IMyData }

 unit MyDataIntf;

 interface

 uses Soap.InvokeRegistry, System.Types, Soap.XSBuiltIns;

 type

   { Invokable interfaces must derive from IInvokable }
IMyData = interface(IInvokable)
['{865DBF5C-8DE1-4D01-AE04-16D04A3F5EF0}']
function GetInfo:widestring;stdcall;
function SetSQL(ASQL: widestring): widestring;stdcall;
{ Methods of Invokable interface must not use the default }
{ calling convention; stdcall is recommended }
end; implementation initialization
{ Invokable interfaces must be registered }
InvRegistry.RegisterInterface(TypeInfo(IMyData)); end.
 { Invokable implementation File for TMyData which implements IMyData }

 unit MyDataImpl;

 interface

 uses Soap.InvokeRegistry, System.Types, Soap.XSBuiltIns, MyDataIntf;

 type

   { TMyData }
TMyData = class(TInvokableClass, IMyData)
public
function GetInfo:widestring;stdcall;
function SetSQL(ASQL: widestring): widestring;stdcall;
end; implementation
uses WebModuleUnit1; { TMyData } function TMyData.GetInfo: widestring;
var
oDM: TWebModule1;
begin
oDM := TWebModule1.Create(nil);
result := oDM.GetInfo;
oDM.Free;
end; function TMyData.SetSQL(ASQL: widestring): widestring;
var
oDM: TWebModule1;
begin
oDM := TWebModule1.Create(nil);
result := oDM.SetSQL(ASQL);
oDM.Free;
end; initialization
{ Invokable classes must be registered }
InvRegistry.RegisterInvokableClass(TMyData);
end.

DelphiXE7客户端具体操作:
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

客户端-实例代码:

 unit Unit1;

 interface

 uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.DBGrids, Data.DB,
Datasnap.DBClient, Vcl.StdCtrls; type
TForm1 = class(TForm)
Button1: TButton;
ClientDataSet1: TClientDataSet;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button2: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation
uses IMyData1;
{$R *.dfm} procedure TForm1.Button1Click(Sender: TObject);
var
ows: IMyData;
s: string;
begin
ows := GetIMyData(true,'http://localhost:8080/wsdl/IMyData',nil); //参数中可以使用配置的url
s := ows.GetInfo;
if length(s) <> then
ClientDataSet1.xmldata := s;
end; procedure TForm1.Button2Click(Sender: TObject);
var
ows:IMyData;
s:string;
begin
ows := GetIMyData(true,'http://localhost:8080/wsdl/IMyData',nil); //参数中可以使用配置的url
s := ows.SetSQL('delete from usesr where yonghu=' + QuotedStr('ni2'));
if length(s) <> then
Edit1.Text := s;
end; end.