使用delphi 10.2 开发linux 上的webservice

简介: 前几天做了linux下apache的开发,今天做一个linux 下的webservice ,以供客户端调用。 闲话少说,直接干。 新建一个工程。选other...,选择如图。 继续输入服务名 然后就生成对应的单元。

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

闲话少说,直接干。

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

继续输入服务名

然后就生成对应的单元。

 增加linux 平台。

 

完善对应的单元代码

{ Invokable implementation File for Txaliontest which implements Ixaliontest }

unit xaliontestImpl;

interface

uses 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;

interface

uses 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;

implementation

initialization
  { 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;

interface

uses 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;

编译运行。

 

 

 

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

我们今天的任务完成了。

 

目录
相关文章
|
5天前
|
Linux 编译器 Android开发
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
在Linux环境下,本文指导如何交叉编译x265的so库以适应Android。首先,需安装cmake和下载android-ndk-r21e。接着,下载x265源码,修改crosscompile.cmake的编译器设置。配置x265源码,使用指定的NDK路径,并在配置界面修改相关选项。随后,修改编译规则,编译并安装x265,调整pc描述文件并更新PKG_CONFIG_PATH。最后,修改FFmpeg配置脚本启用x265支持,编译安装FFmpeg,将生成的so文件导入Android工程,调整gradle配置以确保顺利运行。
24 1
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
|
21天前
|
Linux API C语言
FFmpeg开发笔记(一)搭建Linux系统的开发环境
本文指导初学者如何在Linux上搭建FFmpeg开发环境。首先,由于FFmpeg依赖第三方库,可以免去编译源码的复杂过程,直接安装预编译的FFmpeg动态库。推荐网站<https://github.com/BtbN/FFmpeg-Builds/releases>提供适用于不同系统的FFmpeg包。但在安装前,需确保系统有不低于2.22版本的glibc库。详细步骤包括下载glibc-2.23源码,配置、编译和安装。接着,下载Linux版FFmpeg安装包,解压至/usr/local/ffmpeg,并设置环境变量。最后编写和编译简单的C或C++测试程序验证FFmpeg环境是否正确配置。
37 8
FFmpeg开发笔记(一)搭建Linux系统的开发环境
|
1月前
|
存储 缓存 Linux
探秘Linux块设备驱动程序:成为内核开发大师的第一步
探秘Linux块设备驱动程序:成为内核开发大师的第一步
92 0
|
1月前
|
Linux
linux驱动开发-点亮第一个led灯
linux驱动开发-点亮第一个led灯
23 0
|
1月前
|
网络协议 Java Linux
Java 开发常用的 Linux 命令知识积累
Java 开发常用的 Linux 命令知识积累
38 0
|
1月前
|
网络协议 Ubuntu Linux
「远程开发」VSCode使用SSH远程linux服务器 - 公网远程连接
「远程开发」VSCode使用SSH远程linux服务器 - 公网远程连接
130 0
|
2月前
|
Linux 芯片 开发者
Linux 驱动开发基础知识——内核对设备树的处理与使用(十)
Linux 驱动开发基础知识——内核对设备树的处理与使用(十)
167 0
Linux 驱动开发基础知识——内核对设备树的处理与使用(十)
|
4月前
|
XML Java 应用服务中间件
WebService - Axis2基于JAX-WS开发WebService并发布多个WebService
WebService - Axis2基于JAX-WS开发WebService并发布多个WebService
36 0
|
4月前
|
Java 应用服务中间件 Spring
WebService - Axis2使用services.xml进行开发server与client(未与Spring整合)
WebService - Axis2使用services.xml进行开发server与client(未与Spring整合)
44 0
|
4月前
|
Java 应用服务中间件 Spring
WebService - CXF开发Server和Client(main方法测试)
WebService - CXF开发Server和Client(main方法测试)
40 0