delphi 调用 java web service(100)

  • 主题发起人 主题发起人 jadic
  • 开始时间 开始时间
J

jadic

Unregistered / Unconfirmed
GUEST, unregistred user!
web service 服务端是用cxf简单实现的,java作为客户度已经能成功调用但用Delphi作为客户端调用时出错Delphi实现客户端时,直接用Delphi自带的wsdl importor生成接口文件先把wsdl贴出来吧<?xml version="1.0" encoding="UTF-8" ?> - <wsdl:definitions name="HelloWorldImplService" targetNamespace="http://server.cxf.com/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.cxf.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">- <wsdl:types>- <xs:schema elementFormDefault="unqualified" targetNamespace="http://server.cxf.com/" version="1.0" xmlns:tns="http://server.cxf.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="sayHi" type="tns:sayHi" /> <xs:element name="sayHiResponse" type="tns:sayHiResponse" /> - <xs:complexType name="sayHi">- <xs:sequence> <xs:element minOccurs="0" name="text" type="xs:string" /> </xs:sequence> </xs:complexType>- <xs:complexType name="sayHiResponse">- <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:schema> </wsdl:types>- <wsdl:message name="sayHiResponse"> <wsdl:part element="tns:sayHiResponse" name="parameters" /> </wsdl:message>- <wsdl:message name="sayHi"> <wsdl:part element="tns:sayHi" name="parameters" /> </wsdl:message>- <wsdl:portType name="HelloWorld">- <wsdl:operation name="sayHi"> <wsdl:input message="tns:sayHi" name="sayHi" /> <wsdl:output message="tns:sayHiResponse" name="sayHiResponse" /> </wsdl:operation> </wsdl:portType>- <wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorld"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> - <wsdl:operation name="sayHi"> <soap:operation soapAction="" style="document" /> - <wsdl:input name="sayHi"> <soap:body use="literal" /> </wsdl:input>- <wsdl:output name="sayHiResponse"> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding>- <wsdl:service name="HelloWorldImplService">- <wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort"> <soap:address location="http://192.168.3.19:9199/helloWorld" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
 
delphi生成的接口文件// ************************************************************************ //// The types declared in this file were generated from data read from the// WSDL File described below:// WSDL : http://192.168.3.19:9199/helloWorld?wsdl// Encoding : UTF-8// Version : 1.0// (2010-4-12 16:05:17 - 1.33.2.5)// ************************************************************************ //unit IntfHelloWorld;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://server.cxf.com/ // transport : http://schemas.xmlsoap.org/soap/http // style : document // binding : HelloWorldImplServiceSoapBinding // service : HelloWorldImplService // port : HelloWorldImplPort // URL : http://192.168.3.19:9199/helloWorld // ************************************************************************ // HelloWorld = interface(IInvokable) ['{FD8A0710-486D-67E2-9EDD-8D2655AA62BC}'] function sayHi(const text: WideString): WideString; stdcall; end;function GetHelloWorld(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): HelloWorld;implementationfunction GetHelloWorld(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): HelloWorld;const defWSDL = 'http://192.168.3.19:9199/helloWorld?wsdl'; defURL = 'http://192.168.3.19:9199/helloWorld'; defSvc = 'HelloWorldImplService'; defPrt = 'HelloWorldImplPort';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 HelloWorld); 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(HelloWorld), 'http://server.cxf.com/', 'UTF-8'); InvRegistry.RegisterDefaultSOAPAction(TypeInfo(HelloWorld), ''); InvRegistry.RegisterInvokeOptions(TypeInfo(HelloWorld), ioDocument);end.调用代码:type TForm1 = class(TForm) btn1: TButton; procedure btn1Click(Sender: TObject); private { Private declarations } function GetWebService: HelloWorld; public { Public declarations } property WebService: HelloWorld read GetWebService; end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);begin Caption := GetWebService.sayHi('Jadic');end;function TForm1.GetWebService: HelloWorld;begin Result := IntfHelloWorld.GetHelloWorld;end;end.调用sayHi接口时会出现这个错unexpected element (uri:"http://server.cxf.com/", local:"text"). Expected elements are <{}text>如果我把接口文件中的最后一行的InvRegistry.RegisterInvokeOptions(TypeInfo(HelloWorld), ioDocument);改成InvRegistry.RegisterInvokeOptions(TypeInfo(HelloWorld), ioLiteral);会出现这个错Message part text was not recognized. (Does it exist in service WSDL?)请问是不是接口文件中还有哪个地方注册还没有写,谢谢指点
 
再往上挤下,哪位知道的还请指点下啊
 
没人肯答吗
 
都不需要THTTPRIO?
 
有用到啊,接口文件中的GetHelloWorld用到了啊
 
呵,我刚刚在做这个,我是wsdl importor生成接口文件.pas 再在客户端引用,放上THTTPRIO 控件,设置THTTPRIO的wsdllocation 为你的url 在设置service,port 程序var WebSerivceInterface: WebSerivcePortType;begin WebSerivceInterface:=HTTPRIO1 as WebSerivcePortType; retu:=WebSerivceInterface.sendSMS();//调用接口end;
 
Delphi7不能正常调用太正常不过了。建议使用Delphi 2007来调用,肯定没有问题。
 
D7不能调成功吗,我这调C#的没问题啊,就是调Java的不成功,不知道是怎么回事
 
只引用IntfHelloWorld 不加THTTPRIO 事件执行var Hw: HelloWorld;//可把这个升级成窗体上的全局变量begin hw:=GetHelloWorld;;//hw是全局变量就把这句移到create或者show事件里,要加上try,可能会连接不上服务器报错 Caption := hw.sayHi('Jadic');end;
 
不是说D7不能调WebService,而是对WebService的版本支持比较低,有一些新的特性它就不识别了。
 
但还有奇怪的就是,我调用不需要传参数的接口就能正常,传参数就挂了
 
再往上弄一哈
 
后退
顶部