分析过程:
用soap toolkit的跟踪工具MSSoapT看一下,客户端到底向WebService发送了什么数据:
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body SOAP-ENV:encodingStyle="">
<SOAPSDK1:Add xmlns:SOAPSDK1="http://tempuri.org/" SOAP-ENV:encodingStyle="">
<n1 SOAP-ENV:encodingStyle="">5</n1>
<n2 SOAP-ENV:encodingStyle="">10</n2>
</SOAPSDK1:Add>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
再看看vs.NET调试中,IE浏览器发出的数据(模板):
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Add xmlns="http://tempuri.org/">
<n1>int</n1>
<n2>int</n2>
</Add>
</soap:Body>
</soap:Envelope>
区别在哪里?soaptoolkit的数据中,多了个encodingStyle属性,尽管没有制定值。我们想办法屏蔽这个属性。
在SoapSerializer30的startElement方法中的参数中按照如下方式调用,可以不制定这个属性。
改代码如下:
...
Serializer->startElement("Add","http://tempuri.org/","NONE","");
Serializer->startElement("n1","","NONE","");
Serializer->writeString("5");
Serializer->endElement();
Serializer->startElement("n2","","NONE","");
Serializer->writeString("10");
Serializer->endElement();
Serializer->endElement();
...
但返回结果还是0,看来和encodingStyle无关。看看跟踪情况:
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body SOAP-ENV:encodingStyle="">
<SOAPSDK1:Add xmlns:SOAPSDK1="http://tempuri.org/">
<n1>5</n1>
<n2>10</n2>
</SOAPSDK1:Add>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
问题解决:
比较一下vs.net中发出的请求,差别在哪里?认真看一下,n1和n2没有指定命名空间,那么就指定一下吧。
把代码改成:
Serializer->startElement("Add","http://tempuri.org/","NONE","");
Serializer->startElement("n1","http://tempuri.org/","NONE","");
Serializer->writeString("5");
Serializer->endElement();
Serializer->startElement("n2","http://tempuri.org/","NONE","");
Serializer->writeString("10");
Serializer->endElement();
Serializer->endElement();
测试结果正常了,返回15。
看看这时候发出的xml数据:
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body SOAP-ENV:encodingStyle="">
<SOAPSDK1:Add xmlns:SOAPSDK1="http://tempuri.org/">
<SOAPSDK1:n1>5</SOAPSDK1:n1>
<SOAPSDK1:n2>10</SOAPSDK1:n2>
</SOAPSDK1:Add>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
原因:
也许是VS.NET的问题,也许是SoapToolKit的问题,也许是他提供的例子自身就有问题,也许是我的运行环境有问题,当然,更也许是我还没有真正理解xml的命名空间或没有正确使用WebService。
联系客服