用Delphi7访问.NET 2.0的WebService

版权声明

请尊重原创作品。转载请保持文章完整性,并以超链接形式注明原始作者“tingsking18”和主站点地址,方便其他朋友提问和指正。

 

用VS.NET 2003开发的基于.NET 1.1的WebService,可以用Delphi7正确调用。但同样的方法调用VS.NET 2005开发的基于.NET 2.0的WebService时却发生了错误。查阅资料 http://www.community.borland.com/article/borcon/files/4132/paper/4132.html 发现原来Delphi7客户端虽然支持WebService的RPC|Encoded 和 Document|Literal编码,但 默认的是使用RPC。而.NET 2.0下的WebService却是默认采用Documnet|Literal编码的。因此我们需要显示地声明让Delphi客户端采用Documnet|Literal编码就可以了。在Delphi的WSDL Importer产生了WebService的接口文件中加入如下行:

initialization
  InvRegistry.RegisterInterface(TypeInfo(ServiceSoap), 
' http://tempuri.org/ ' ' utf-8 ' );
  InvRegistry.RegisterInvokableClass(ServiceSoapImpl);
  InvRegistry.RegisterDefaultSOAPAction(TypeInfo(ServiceSoap), 
' http://tempuri.org/HelloWorld ' );
  InvRegistry.RegisterInvokeOptions(TypeInfo(ServiceSoap), ioDocument);
// 指明Delphi采用Document编码
end. 

 Delphi端还是按照以前的代码书写即可:

  uses
       Service;
    ......
   var
  a:ServiceSoap;
begin
  a :
=  GetServiceSoap;
  Caption :
=  a.HelloWorld;
end;

如果需要在WebService中传送汉字的参数,则建议在Service.pas中再添加一行代码

function GetServiceSoap(UseWSDL: Boolean; Addr:  string ; HTTPRIO: THTTPRIO): ServiceSoap;
const
  defWSDL 
=   ' http://localhost/myservice/service.asmx?wsdl ' ;
  defURL  
=   ' http://localhost/myservice/service.asmx ' ;
  defSvc  
=   ' Service ' ;
  defPrt  
=   ' ServiceSoap ' ;
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;
  RIO.HTTPWebNode.UseUTF8InHeader :
= 'UTF-8' // 添加该行,指定采用UTF-8代码传输
   try
    Result :
=  (RIO  as  ServiceSoap);
    
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;

 

 需要说明的是使用Delphi2006则不需要上述的改变即可以正确调用VS.NET 2005书写的WebService.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值