读书人

为什么Delphi 6调用C#的WebService只

发布时间: 2012-03-22 17:43:57 作者: rapoo

为什么Delphi 6调用C#的WebService,只要调用传入参数是string类型的函数就报错?
SOAP Response Packet: result element expected, received "<ConfrimResponse xmlns="http://tempuri.org/"/>"

WebService应该没问题,用C#调完全正常,而且用Delphi调,不需要string参数的函数都没问题,返回类型是string也没问题。

D6是古董了点,但暂时不能动,请高手们提供个解决方法。

[解决办法]
好像WebService返回的都是WideString。但这两个类型是否可强制转换,不知道了。
[解决办法]
转为Pchar类型传入试试
[解决办法]
PChar类型可以,
String是第一位是字符串的长度,从第二位开始才是真正的数据
[解决办法]
估计是字符集的问题吧,用utf-8试试看
[解决办法]
用WideString
[解决办法]
两种数据类型不一样要转换一下.
[解决办法]
好像没那么复杂,这是我的应用:

Delphi(Pascal) code
//以下是Delphi生成的WebService调用unit version;interfaceuses InvokeRegistry, SOAPHTTPClient, Types, XSBuiltIns;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 Borland 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"  // ************************************************************************ //  // Namespace : http://csyangpeng.cn/  // soapAction: http://csyangpeng.cn/%operationName%  // transport : http://schemas.xmlsoap.org/soap/http  // binding   : VersionSoap  // service   : Version  // port      : VersionSoap  // URL       : http://www.MyService.com/services/version.asmx  // ************************************************************************ //  VersionSoap = interface(IInvokable)  ['{B274BBBB-F68D-7338-454D-6E33B840DEBE}']    function  GetDataURL: WideString; stdcall;    function  GetSSJKURL: WideString; stdcall;    function  GetVersion: WideString; stdcall;    function  GetDataVersion: WideString; stdcall;    procedure SetDataVersion; stdcall;  end;function GetVersionSoap(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): VersionSoap;implementationuses UniMain;function GetVersionSoap(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): VersionSoap;const  defSvc  = 'Version';  defPrt  = 'VersionSoap';var  RIO: THTTPRIO;begin  Result := nil;  if (Addr = '') then  begin    if UseWSDL then      //Addr := defWSDL      Addr := MainForm.edtWebService.Text+'?wsdl'    else      //Addr := defURL;      Addr := MainForm.edtWebService.Text;  end;  if HTTPRIO = nil then    RIO := THTTPRIO.Create(nil)  else    RIO := HTTPRIO;  try    Result := (RIO as VersionSoap);    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(VersionSoap), 'http://csyangpeng.cn/', 'utf-8');  InvRegistry.RegisterDefaultSOAPAction(TypeInfo(VersionSoap), 'http://csyangpeng.cn/%operationName%');end.
[解决办法]
要看你接口单元中confirm里面生成的是什么类型的了,最好把代码贴出来看看


[解决办法]
在function GetServiceSoap(UseWSDL: Boolean=System.False; Addr: string=''): ServiceSoap;中,最好加上HTTPRIO,即
function GetServiceSoap(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): ServiceSoap;
以前用的时候总感觉动态创建HTTPRIO会有问题,所以最好能在窗体上放一个HTTPRIO,调用的时候带过去

Delphi(Pascal) code
function GetServiceSoap(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): ServiceSoap;var  RIO: THTTPRIO;  defWSDL, defURL, defSvc, defPrt: string;begin  CoInitialize(nil);  if HTTPRIO <> nil then  begin    With HTTPRIO do    begin      defWSDL = 'http://10.16.66.16/KiTest/service.asmx?wsdl';      defURL  = 'http://10.16.66.16/KiTest/service.asmx';      defSvc  = 'Service';      defPrt  = 'ServiceSoap';    end;  end;  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 IWebServiceAPI);    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;  CoUnInitialize;end; 

读书人网 >.NET

热点推荐