博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用delphi 10.2 开发linux 上的webservice
阅读量:4330 次
发布时间:2019-06-06

本文共 5392 字,大约阅读时间需要 17 分钟。

前几天做了linux下apache的开发,今天做一个linux 下的webservice ,以供客户端调用。

闲话少说,直接干。

新建一个工程。选other...,选择如图。

继续输入服务名

然后就生成对应的单元。

 增加linux 平台。

 

完善对应的单元代码

{
Invokable implementation File for Txaliontest which implements Ixaliontest }unit xaliontestImpl;interfaceuses Soap.InvokeRegistry, System.Types, Soap.XSBuiltIns, xaliontestIntf;type {
Txaliontest } Txaliontest = class(TInvokableClass, Ixaliontest) public function echoname(const Value:string):string; stdcall; function sumall(const Value:integer):integer; stdcall; end;implementation{
Txaliontest }function Txaliontest.echoname(const Value: string): string;begin result:='你好'+value;end;function Txaliontest.sumall(const Value: integer): integer;var i:integer; isumall:integer;begin isumall:=0; for i := 1 to value do isumall:=isumall+i; result:=isumall;end;initialization{
Invokable classes must be registered } InvRegistry.RegisterInvokableClass(Txaliontest);end.
{
Invokable interface Ixaliontest }unit xaliontestIntf;interfaceuses Soap.InvokeRegistry, System.Types, Soap.XSBuiltIns;type {
Invokable interfaces must derive from IInvokable } Ixaliontest = interface(IInvokable) ['{20590E4A-BF8C-41AE-A630-94D2DD38EEE1}'] {
Methods of Invokable interface must not use the default } {
calling convention; stdcall is recommended } function echoname(const Value:string):string; stdcall; function sumall(const Value:integer):integer; stdcall; end;implementationinitialization {
Invokable interfaces must be registered } InvRegistry.RegisterInterface(TypeInfo(Ixaliontest));end.

编译本工程。

哎呀,怎么出错了?

不要害怕,这是因为linux 上apache的引用没有加入。我们确认apache 已经在linux上安装成功。

那么我们在IDE 里面处理一下。

记住设以上的地方为True, 允许未定义的引用。

现在重新编译

哈哈,OK 了

现在的任务就是在发布这个apache 模块。

这一块的内容请参照前面的文章。

配置文件如图:

 

 

 

启动apache.

 在浏览器里面输入对应的地址,就可以显示webservice 的接口信息了。

 

 好了,服务端搞定了。我们做一个客户端来调用一下这个服务。

直接建一个vcl application .

然后选择WDSL 导入器。

 

输入对应的地址。

 系统会生成对应的接口文件

// ************************************************************************ //// The types declared in this file were generated from data read from the// WSDL File described below:// WSDL     : http://192.168.1.66/xalionws/wsdl/Ixaliontest// Encoding : utf-8// Version  : 1.0// (2017-4-8 21:33:51 - - $Rev: 90173 $)// ************************************************************************ //unit Ixaliontest1;interfaceuses Soap.InvokeRegistry, Soap.SOAPHTTPClient, System.Types, Soap.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 Embarcadero types; however, they could also   // indicate incorrect WSDL documents that failed to declare or import a schema type.  // ************************************************************************ //  // !:int             - "http://www.w3.org/2001/XMLSchema"[]  // !:string          - "http://www.w3.org/2001/XMLSchema"[]  // ************************************************************************ //  // Namespace : urn:xaliontestIntf-Ixaliontest  // soapAction: urn:xaliontestIntf-Ixaliontest#%operationName%  // transport : http://schemas.xmlsoap.org/soap/http  // style     : rpc  // use       : encoded  // binding   : Ixaliontestbinding  // service   : Ixaliontestservice  // port      : IxaliontestPort  // URL       : http://192.168.1.66/xalionws/soap/Ixaliontest  // ************************************************************************ //  Ixaliontest = interface(IInvokable)  ['{A3FB1A48-1AE7-B449-16DC-42CCE1A48832}']    function  echoname(const Value: string): string; stdcall;    function  sumall(const Value: Integer): Integer; stdcall;  end;function GetIxaliontest(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): Ixaliontest;implementation  uses System.SysUtils;function GetIxaliontest(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): Ixaliontest;const  defWSDL = 'http://192.168.1.66/xalionws/wsdl/Ixaliontest';  defURL  = 'http://192.168.1.66/xalionws/soap/Ixaliontest';  defSvc  = 'Ixaliontestservice';  defPrt  = 'IxaliontestPort';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 Ixaliontest);    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  {
Ixaliontest } InvRegistry.RegisterInterface(TypeInfo(Ixaliontest), 'urn:xaliontestIntf-Ixaliontest', 'utf-8'); InvRegistry.RegisterDefaultSOAPAction(TypeInfo(Ixaliontest), 'urn:xaliontestIntf-Ixaliontest#%operationName%');end.

在主窗体放两个按钮。

 

代码如下

implementation{
$R *.dfm}uses Ixaliontest1;procedure TForm2.Button1Click(Sender: TObject);begin showmessage( GetIxaliontest.echoname('xalion'));end;procedure TForm2.Button2Click(Sender: TObject);begin showmessage( GetIxaliontest.sumall(100).tostring);end;

编译运行。

 

 

 

可以看见,客户端很顺利的调用了服务器端的函数。

我们今天的任务完成了。

 

转载于:https://www.cnblogs.com/xalion/p/6683165.html

你可能感兴趣的文章
使用Gitbook来编写你的Api文档
查看>>
jquery扩展 $.fn
查看>>
Markdown指南
查看>>
influxDB的安装和简单使用
查看>>
JPA框架学习
查看>>
JPA、JTA、XA相关索引
查看>>
机器分配
查看>>
php opcode缓存
查看>>
springcloud之Feign、ribbon设置超时时间和重试机制的总结
查看>>
Go 结构体
查看>>
LINQ巩固
查看>>
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
查看>>
UIDynamic(物理仿真)
查看>>
Windows下安装Redis
查看>>
迷宫实现
查看>>
【字符编码】Java字符编码详细解答及问题探讨
查看>>
学习操作系统导图
查看>>
在线的JSON formate工具
查看>>
winform非常实用的程序退出方法!!!!!(转自博客园)
查看>>
xml解析
查看>>