我在接收来自 Delphi 7 客户端的无效非 ascii 字符时遇到问题,该客户端将 utf-8 编码的 XML 发送到字符串参数中的 C# WebService。使用 .Net 客户端,可以毫无问题地接收字符。我尝试了很多方法,但似乎都不起作用,因此我决定使用 Wireshark 跟踪 SOAP 对话。对于 .Net 客户端,SOAP 信封的 XML 采用 utf-8 编码,如下所示:
POST //TesteService/ServicoAgente.asmx HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.3615)
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://example.com/ValidarTransacao"
Host: 192.168.254.2
Content-Length: 1530
Expect: 100-continue
Connection: Keep-Alive
HTTP/1.1 100 Continue
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body> .....
然而,对于Delphi 7 SOAP header,不包括编码头,如下:
POST /TesteService/ServicoAgente.asmx HTTP/1.1
SOAPAction: "http://example.com/ValidarTransacao"
Content-Type: text/xml
User-Agent: Borland SOAP 1.2
Host: 192.168.254.1
Content-Length: 1548
Connection: Keep-Alive
Cache-Control: no-cache
<?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> .....
我的 XML 作为参数包含在 SOAP 主体内,其中包含一些显示为 ?? 的非 ascii 字符。在我收到来自 Delphi 客户端的方法后,在 C# Web 服务中,这在我看来是唯一合乎逻辑的解释。当我在发送之前在 Delphi 中检查 XML 时,它的编码是完美的,但是在接收端,我得到 ??。
在使用 WSDL 导入器导入的单元上,它指定 utf-8,如下所示:
initialization
InvRegistry.RegisterInterface(TypeInfo(TesteAgentSoap), 'http://example.com/', 'utf-8');
InvRegistry.RegisterDefaultSOAPAction(TypeInfo(TesteAgentSoap), 'http://example.com/%operationName%');
InvRegistry.RegisterInvokeOptions(TypeInfo(TesteAgentSoap), [ioDefault, ioDocument, ioHasReturnParamNames, ioHasNamespace]);
end.
如何更改 SOAP 信封的 XML 编码?任何想法还有什么可能是错误的?我认为合乎逻辑的是,如果 SOAP 信封的 XML 未正确编码为 utf-8,那么该 XML 中的 XML 将无法正确读取
非常感谢您的宝贵时间