您的位置:首页 > 编程语言 > Delphi

Delphi调用基于CXF创建的Web Service问题

2009-08-19 19:18 513 查看
1. Encoding编码问题

2009-8-19 19:19:49 org.apache.cxf.interceptor.LoggingInInterceptor logging
信息: Inbound Message
----------------------------
ID: 1
Address: /HelloWorld
Encoding: ISO-8859-1
Content-Type: text/xml
Headers: {content-type=[text/xml], connection=[Keep-Alive], Host=[192.168.3.101], Content-Length=[500], SOAPAction=[""], User-Agent=[Borland SOAP 1.2], Content-Type=[text/xml], Cache-Control=[no-cache]}
Payload: <?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><NS1:sayHi xmlns:NS1="cds.ws"><aName xsi:type="xsd:string">OK</aName><bName xsi:type="xsd:string">not</bName></NS1:sayHi></SOAP-ENV:Body></SOAP-ENV:Envelope>

--------------------------------------
2009-8-19 19:19:49 org.apache.cxf.interceptor.LoggingOutInterceptor$LoggingCallback onClose
信息: Outbound Message
---------------------------
ID: 1
Encoding: ISO-8859-1
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:sayHiResponse xmlns:ns1="cds.ws"><return>你好, OK, not</return></ns1:sayHiResponse></soap:Body></soap:Envelope>
--------------------------------------

Delphi使用WSDLImp.exe导入接口后生成的接口文件中添加

...

Result := (RIO as IHelloWorld);
if UseWSDL then
begin
RIO.WSDLLocation := Addr;
RIO.Service := defSvc;
RIO.Port := defPrt;
end else
RIO.URL := Addr;
RIO.HTTPWebNode.UseUTF8InHeader := true; //<- 添加

添加语句以后
Encoding: UTF-8

2. 调用方法返回null值问题

调用不成功的消息

2009-8-19 18:50:22 org.apache.cxf.interceptor.LoggingInInterceptor logging
信息: Inbound Message
----------------------------
ID: 1
Address: /HelloWorld
Encoding: UTF-8
Content-Type: text/xml; charset="utf-8"
Headers: {content-type=[text/xml; charset="utf-8"], connection=[Keep-Alive], Host=[192.168.3.101], Content-Length=[318], SOAPAction=[""], User-Agent=[Borland SOAP 1.2], Content-Type=[text/xml; charset="utf-8"], Cache-Control=[no-cache]}
Payload: <?xml version="1.0"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Body><sayHi xmlns="cds.ws"><aName>OK</aName><bName>not</bName></sayHi></SOAP-ENV:Body></SOAP-ENV:Envelope>

--------------------------------------
2009-8-19 18:50:22 org.apache.cxf.interceptor.LoggingOutInterceptor$LoggingCallback onClose
信息: Outbound Message
---------------------------
ID: 1
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:sayHiResponse xmlns:ns1="cds.ws"><return>你好, null, null</return></ns1:sayHiResponse></soap:Body></soap:Envelope>
--------------------------------------

调用成功的消息

2009-8-19 19:19:49 org.apache.cxf.interceptor.LoggingInInterceptor logging

信息: Inbound Message

----------------------------

ID: 1

Address: /HelloWorld

Encoding: ISO-8859-1

Content-Type: text/xml

Headers: {content-type=[text/xml], connection=[Keep-Alive], Host=[192.168.3.101], Content-Length=[500], SOAPAction=[""], User-Agent=[Borland SOAP 1.2], Content-Type=[text/xml], Cache-Control=[no-cache]}

Payload: <?xml version="1.0"?>

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><NS1:sayHi xmlns:NS1="cds.ws"><aName xsi:type="xsd:string">OK</aName><bName xsi:type="xsd:string">not</bName></NS1:sayHi></SOAP-ENV:Body></SOAP-ENV:Envelope>

--------------------------------------

2009-8-19 19:19:49 org.apache.cxf.interceptor.LoggingOutInterceptor$LoggingCallback onClose

信息: Outbound Message

---------------------------

ID: 1

Encoding: ISO-8859-1

Content-Type: text/xml

Headers: {}

Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:sayHiResponse xmlns:ns1="cds.ws"><return>你好, OK, not</return></ns1:sayHiResponse></soap:Body></soap:Envelope>

--------------------------------------

调用不成功是由于Delphi发送的SOAP包中的命名空间与服务器上的命名空间不一致。

Delphi使用WSDLImp.exe导入接口后生成的接口文件中添加soRootRefNodesToBody以后就可以正常返回结果了

...
try
Result := (RIO as IHelloWorld);
if UseWSDL then begin
RIO.WSDLLocation := Addr;
RIO.Service := defSvc;
RIO.Port := defPrt;
end else
RIO.URL := Addr;
RIO.HTTPWebNode.UseUTF8InHeader := true;
//添加生成SOAP消息格式的选择,可以解决返回null值问题。.net中可以可以设置soRootRefNodesToBody解决返回null问题
RIO.Converter.Options := [soSendMultiRefObj, soTryAllSchema, soRootRefNodesToBody,
soUTF8InHeader, soCacheMimeResponse, soUTF8EncodeXML];
finally
if (Result = nil) and (HTTPRIO = nil) then
RIO.Free;
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: