使用ASP.NET调用PHP SOAP WebService

5

我在尝试使用ASP.NET消费PHP SOAP webservice时遇到了一些重大问题。所涉及的webservice基于PHP SOAP扩展,并由以下WSDL描述:

<?xml version="1.0" encoding="UTF-8" ?>
<definitions name="MyServices"
  targetNamespace="http://mydomain.com/api/soap/v11/services"
  xmlns:tns="http://mydomain.com/api/soap/v11/services"
  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsd1="http://mydomain.com/api/soap/v11/services"
  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
  xmlns="http://schemas.xmlsoap.org/wsdl/">

<types>
    <schema targetNamespace="http://mydomain.com/api/soap/v11/services" xmlns="http://www.w3.org/2001/XMLSchema">
        <complexType name="ServiceType">
            <all>
                <element name="id" type="xsd:int" minOccurs="1" maxOccurs="1" />
                <element name="name" type="xsd:string" minOccurs="1" maxOccurs="1" />
                <element name="cost" type="xsd:float" minOccurs="1" maxOccurs="1" />
            </all>
        </complexType>
        <complexType name="ArrayOfServiceType">
            <all>
                <element name="Services" type="ServiceType" minOccurs="0" maxOccurs="unbounded" />
            </all>
        </complexType>
    </schema>
</types>


<message name="getServicesRequest">
    <part name="postcode" type="xsd:string" />
</message>

<message name="getServicesResponse">
  <part name="Result" type="xsd1:ArrayOfServiceType"/>
</message>

<portType name="ServicesPortType">
  <operation name="getServices">
    <input message="tns:getServicesRequest"/>
    <output message="tns:getServicesResponse"/>
  </operation>
</portType>

<binding name="ServicesBinding" type="tns:ServicesPortType">
  <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
  <operation name="getServices">
    <soap:operation soapAction="http://mydomain.com/api/soap/v11/services/getServices" />
    <input>
      <soap:body use="encoded" namespace="urn:my:services" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
    </input>
    <output>
      <soap:body use="encoded" namespace="urn:my:services" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
    </output>
  </operation>
</binding>

<service name="MyServices">
  <port name="ServicesPort" binding="tns:ServicesBinding">
    <soap:address location="http://mydomain.com/api/soap/v11/services"/>
  </port>
</service>
</definitions>

我可以在Visual Studio中成功生成此WSDL的代理类,但尝试调用getServices方法时,出现了异常:
System.Web.Services.Protocols.SoapHeaderException: Procedure 'string' not present
检查SOAP服务器端的原始POST数据后,我的PHP SOAP客户端发出的请求如下:
<?xml version="1.0" encoding="UTF-8"?>
<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:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <postcode xsi:type="xsd:string">ln4 4nq</postcode>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

而 .Net 代理类正在执行以下操作:
<?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://mydomain.com/api/soap/v11/services" 
    xmlns:types="http://mydomain.com/api/soap/v11/services/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/">
        <xsd:string xsi:type="xsd:string">LN4 4NQ</xsd:string>
    </soap:Body>
</soap:Envelope>

我只能假设邮政编码参数发送的方式不同是问题所在,但作为一名主要从事PHP开发的人,我对这里发生的事情感到困惑。我有一种感觉,在我的WSDL中可能缺少了一些至关重要的内容,因为我看过无数个“使用.Net消费PHP SOAP WebServices”的例子,这些例子似乎表明它“只需要运行”。如果您能提出任何建议,指出我在哪里犯了错误,我将不胜感激。我现在已经花了将近一整天的时间来解决这个问题;-)谢谢您的帮助,Jamie

在 SOAP 上失误是很容易的... :-D - Andy Chase
2个回答

1

很抱歉我只能回答而不能留言,因为我还没有足够的声望...

你应该尝试使用SOAP-UI(他们有免费版本)来测试你的Web服务,这样你就会知道问题是客户端还是服务器端引起的。


0

WSDL存在错误,因此代理类很可能已经损坏。请求也是如此。
除非使用好的WSDL编辑器,否则正确处理WSDL非常复杂。我建议使用Eclipse WSDL编辑器(http://wiki.eclipse.org/index.php/Introduction_to_the_WSDL_Editor

(1)“all”模型组中元素的{max occurs}必须为0或1。“Services”元素的值“-1”无效。
(2)解析组件“ServiceType”时出错。检测到“ServiceType”在命名空间“http://www.w3.org/2001/XMLSchema”中,但是该命名空间中的组件无法从模式文档中引用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接