码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2020-03-18 15:32:07      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:工程   class   错误   EDA   stand   server   utils   schema   graph   

DelphiXE3新建WebService具体操作
1.打开“DelphiXE3”->“File”->“New”->“Other”
2.“New Items”->“Delphi Projects”->“WebSrvice”->“SOAP Server Application”
3.“Stand-alone application”->“Next”
4.“VCL application”->“Next”
5.“8082”->“Finish” 端口可以自行修改,防止与tomcat的冲突,建议不要用8080端口
6.“Create Interface for SOAPmodule?”->“Yes”
7.“Add New WebService”->输入服务名字“MyData”->“OK”
8.保存全部工程文件
9.在“WebModuleUnit1”单元中放入控件: 我是需要连接oracle,所用用了ODAC的组件,其他也可以选择ADO或是fireDAC
TOraSession
TOraQuery
DataSetProvider1
ClientDataSet1
10.双击oracSession 设置好本地连接,并测试通。
11.TOraQuery1->session:oracSession
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,
  Data.DB, MemDS, DBAccess, Ora, Datasnap.DBClient, Datasnap.Provider;

type
  TWebModule1 = class(TWebModule)
    HTTPSoapDispatcher1: THTTPSoapDispatcher;
    HTTPSoapPascalInvoker1: THTTPSoapPascalInvoker;
    WSDLHTMLPublish1: TWSDLHTMLPublish;
    dtstprvdr1: TDataSetProvider;
    ds1: TClientDataSet;
    orsn1: TOraSession;
    orqry1: TOraQuery;
    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

{$R *.dfm}

function TWebModule1.GetInfo: widestring;
begin
  ds1.Close;
  ds1.Open;
  Result := ds1.XMLData;
  ds1.Close;
end;

function TWebModule1.SetSQL(ASQL: widestring): widestring;
begin
  orqry1.Close;
  orqry1.SQL.Text := ASQL;
  try
    orqry1.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)
  [‘{A95FAF6B-C38B-4FE9-8B8E-23DF5983D30E}‘]
   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.

 

 

DelphiXE3中找不到wsdl importer,改到delphi 7 中进行,具体操作:
1.打开“Delphi7”->“File”->“New”->“Other”
2.“New Items”->“Delphi Projects”->“WebSrvice”->“WSDL Importer”
3.“Import WSDL”->WSDL Source中输入“http:/172.16.10.41:8082/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
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, DBGrids, DB, DBClient;

type
  TForm1 = class(TForm)
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses
  IMyData1;

procedure TForm1.Button1Click(Sender: TObject);
var
  ows: IMyData;
  s: string;
begin
  ows := GetIMyData(true, ‘http://172.16.10.41:8082/wsdl/IMyData‘, nil);   //参数中可以使用配置的url
  s := ows.GetInfo;
  if length(s) <> 0 then
    ClientDataSet1.xmldata := s;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  ows: IMyData;
  s: string;
begin
  if Edit1.Text = ‘‘ then
  begin
    Application.MessageBox(‘请输入要删除的员工姓名‘, ‘操作错误‘);
    exit;
  end;
  ows := GetIMyData(true, ‘http://172.16.10.41:8082/wsdl/IMyData‘, nil);   //参数中可以使用配置的url
  s := ows.SetSQL(‘delete from employee where 姓名=‘ + QuotedStr(Edit1.Text));
  if length(s) <> 0 then
    Application.MessageBox(‘已删除成功‘, ‘提示‘);
end;

end.

 

编译并运行服务端:

技术图片

 

 

技术图片

 

 

运行客户端:

技术图片

 

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

标签:工程   class   错误   EDA   stand   server   utils   schema   graph   

原文地址:https://www.cnblogs.com/zjy2020/p/12517297.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!