webservice 调用java_Delphi调用Java WebService

刚用Java做了个WebService,并且在Java中调用成功, WebService在Delphi中同样可以调用。

在Delphi中新建一个工程,然后点击Component->Import WSDL...

686c272dd6c3237978d824e137cc3239.png

WSDL的URL以及该WebService的内容请参考:

使用缺省设置,生成引用文件:

// ************************************************************************ //

// The types declared in this file were generated from data read from the

// WSDL File described below:

// WSDL     : http://localhost:8030/garfield.MyJ6WebService?wsdl

//  >Import : http://localhost:8030/garfield.MyJ6WebService?wsdl>0

//  >Import : http://localhost:8030/garfield.MyJ6WebService?xsd=1

// Encoding : UTF-8

// Version  : 1.0

// (2012-09-14 9:39:27 - - $Rev: 25127 $)

// ************************************************************************ //

unit garfield;

interface

uses InvokeRegistry, SOAPHTTPClient, Types, XSBuiltIns;

const

IS_OPTN = $0001;

IS_UNQL = $0008;

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.

// ************************************************************************ //

// !:string          - "http://www.w3.org/2001/XMLSchema"[Gbl]

// ************************************************************************ //

// Namespace : http://garfield/

// transport : http://schemas.xmlsoap.org/soap/http

// style     : document

// binding   : MyJ6WebServicePortBinding

// service   : MyJ6WebServiceService

// port      : MyJ6WebServicePort

// URL       : http://localhost:8030/garfield.MyJ6WebService

// ************************************************************************ //

MyJ6WebService = interface(IInvokable)

['{73C2D68E-4BCE-E05A-3459-8E5DFD772DC1}']

function  SayHello(const arg0: string): string; stdcall;

end;

function GetMyJ6WebService(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): MyJ6WebService;

implementation

uses SysUtils;

function GetMyJ6WebService(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): MyJ6WebService;

const

defWSDL = 'http://localhost:8030/garfield.MyJ6WebService?wsdl';

defURL  = 'http://localhost:8030/garfield.MyJ6WebService';

defSvc  = 'MyJ6WebServiceService';

defPrt  = 'MyJ6WebServicePort';

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 MyJ6WebService);

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

InvRegistry.RegisterInterface(TypeInfo(MyJ6WebService), 'http://garfield/', 'UTF-8');

InvRegistry.RegisterDefaultSOAPAction(TypeInfo(MyJ6WebService), '');

//注意:下面系统自动生成,如果不注释掉,传人的参数将全部为null,不清楚什么原因,请大侠指教!

//InvRegistry.RegisterInvokeOptions(TypeInfo(MyJ6WebService), ioDocument);

end.

剩下的工作很简单了,简单调用:

ShowMessage(GetMyJ6WebService().SayHello('garfield'));

就可以在对话框中显示:

Hello,Garfiled !

需要注意的是,在生成的接口文件中:

initialization

InvRegistry.RegisterInterface(TypeInfo(MyJ6WebService), 'http://garfield/', 'UTF-8');

InvRegistry.RegisterDefaultSOAPAction(TypeInfo(MyJ6WebService), '');

//注意:下面系统自动生成,如果不注释掉,传人的参数将全部为null,不清楚什么原因,请大侠指教!

//InvRegistry.RegisterInvokeOptions(TypeInfo(MyJ6WebService), ioDocument);

上面这行要注释掉,要不然传人的参数会变成null,即返回:

Hello,null !

上面在Delphi 2010上测试通过。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值