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上測試通過。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM