用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.