Delphi调用Java WebService

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

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

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

http://www.cnblogs.com/GarfieldTom/archive/2012/09/14/2684361.html

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

// ************************************************************************ //
// 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): stringstdcall;
  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 = niland (HTTPRIO = nilthen
      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上测试通过。
原文地址:https://www.cnblogs.com/GarfieldTom/p/2684461.html