我正在编写一个需要调用外部网络服务的 .NET 网络应用程序。我获得的文档包括 PHP 代码示例。
我可以使用提供给我的 WSDL 地址在 VS2010 中成功创建 Web 引用,并且使用 fiddler 我可以看到预期的 XML 正在发送和接收。然而,.NET 似乎无法解析返回的 XML。
我正在处理的最简单的网络服务只接受一组用户名,并返回一些嵌套的用户哈希数组(每个用户都有自己的数组名称、类型等字段)和一组错误(对于任何不匹配的用户名)。我的文档用“PHP-ish”描述了它:
array (
'users' => array (
array(
'id' => 11,
'username' => 'mick',
'firstname' => 'Mick',
'lastname' => 'Byrne'
),
...
)
'errors' => array(
array(
'username' => 'whoever',
'errorcode' => 'NOSUCHUSER'
)
)
)
我正在获取对应于此的 SOAP XML。但是,当 .NET 尝试将其转换为结果时,它会抛出异常:
无法将 System.Xml.XmlNode[] 类型的对象分配给 System.String 类型的对象。
有趣的是,.NET 基于 WSDL 为我创建的相应方法说它返回一个普通的旧 string
,这表明它无法处理 WSDL 定义返回类型的方式。
完整的 WSDL 可在此处获得:
这是从这个基本的“获取用户名”请求中发送和返回的 XML 示例:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://www.elearning.psychology.org.au/webservice/soap/server.php?wstoken=dc45858adb6f28b7feae87014d46d9b3" xmlns:types="http://www.elearning.psychology.org.au/webservice/soap/server.php?wstoken=dc45858adb6f28b7feae87014d46d9b3/encodedTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<tns:netspot_user_get_users_by_username>
<usernames href="#id1" />
</tns:netspot_user_get_users_by_username>
<soapenc:Array id="id1" soapenc:arrayType="xsd:string[1]">
<Item>557788</Item>
</soapenc:Array>
</soap:Body>
</soap:Envelope>
和响应:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.elearning.psychology.org.au/webservice/soap/server.php?wstoken=dc45858adb6f28b7feae87014d46d9b3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:netspot_user_get_users_by_usernameResponse>
<return xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">errors</key>
<value SOAP-ENC:arrayType="ns2:Map[1]" xsi:type="SOAP-ENC:Array">
<item xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">username</key>
<value xsi:type="xsd:string">557788</value>
</item>
<item>
<key xsi:type="xsd:string">errorcode</key>
<value xsi:type="xsd:string">NOSUCHUSER</value>
</item>
</item>
</value>
</item>
</return>
</ns1:netspot_user_get_users_by_usernameResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
如有任何帮助,我们将不胜感激。
最佳答案
遇到了同样的问题。我所要做的就是修复生成的 cs 文件中从 https
到 http
的每个命名空间。所以,可能是您的 namespace 有误。
关于php - 使用返回散列数组的外部 PHP SOAP Web 服务的 C# .NET 客户端出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5467024/