使用返回哈希数组的外部 PHP SOAP Web 服务的 C# .NET 客户端存在问题
我正在编写一个需要调用外部 Web 服务的 .NET Web 应用程序。我提供的文档包括 PHP 代码示例。
我可以使用提供给我的 WSDL 地址在 VS2010 中成功创建 Web 引用,并且使用 fiddler 我可以看到正在发送和接收预期的 XML。但是 .NET 似乎在解析返回的 XML 时遇到问题。
我正在处理的最简单的 Web 服务只接受一个用户名数组,并返回一些用户的嵌套哈希数组(每个用户都有自己的数组名称、类型等字段)和一个错误数组(对于任何用户名)不匹配)。我的文档以“PHP-ish”对其进行了描述:
array (
'users' => array (
array(
'id' => 11,
'username' => 'mick',
'firstname' => 'Mick',
'lastname' => 'Byrne'
),
...
)
'errors' => array(
array(
'username' => 'whoever',
'errorcode' => 'NOSUCHUSER'
)
)
)
我正在获取与此相对应的 SOAP XML。然而,当.NET尝试将其转换为结果时,它会抛出异常:
Cannot allocate object of type System.Xml.XmlNode[] to an object of type System.String.
有趣的是,相应的方法.NET 根据 WSDL 为我创建的文件表示它返回一个普通的旧字符串,这表明它无法处理 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>
//www.elearning.psychology.org.au/webservice/soap/server.php?wsdl=1&wstoken=dc45858adb6f28b7feae87014d46d9b3 回复:
<?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>
任何帮助将不胜感激。
I am writing a .NET web application that needs to call external webservices. The documentation I have been provided with includes code examples in PHP.
I can successfully create a web reference in VS2010 using the WSDL address that has been provided to me, and using fiddler I can see that the expected XML is getting sent and received. However .NET appears to be having trouble parsing the returned XML.
The simplest web service I'm dealing with just accepts an array of usernames and is meant to return some nested hash arrays of users (with each user it's own array name, type, etc. fields) and an array of errors (for any usernames that didn't match up). The documentation I have describes it in 'PHP-ish':
array (
'users' => array (
array(
'id' => 11,
'username' => 'mick',
'firstname' => 'Mick',
'lastname' => 'Byrne'
),
...
)
'errors' => array(
array(
'username' => 'whoever',
'errorcode' => 'NOSUCHUSER'
)
)
)
I'm getting the SOAP XML that would correspond to this. However, when .NET tries to turn it into a result it throws an exception:
Cannot assign object of type System.Xml.XmlNode[] to an object of type System.String.
Interestingly, the corresponding method that .NET has created for me based on the WSDL says it returns a plain old string
which suggests that it can't handle the way the WSDL defines the return type.
The full WSDL is available here:
Here is a sample of the sent and returned XML from the this basic Get Usernames request:
<?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>
And response:
<?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>
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有同样的问题。我所要做的就是修复生成的 cs 文件中从
https
到http
的每个命名空间。所以,可能是你的命名空间是错误的。Had the same problem. All I had to do is fix every namespace from
https
tohttp
in generated cs file. So, it could be your namespaces are wrong.