我在这个特定的MATLAB示例中使用SOAP的方式有什么问题?

时间:2012-08-14 11:50:37

标签: web-services debugging matlab dom soap

我正在尝试使用以下MATLAB代码连接到http://www.webservicex.net/的示例soap服务器:

% createSoapMessage(NAMESPACE,METHOD,VALUES,NAMES,TYPES,STYLE) creates a SOAP message.
% VALUES, NAMES, and TYPES are cell arrays.  
m = createSoapMessage('http://www.webserviceX.NET', 'GetCitiesByCountry', ...
  {'Australia'}, {'CountryName'}, { '{http://www.w3.org/2001/XMLSchema}string' }, 'rpc')

%    callSoapService(ENDPOINT,SOAPACTION,MESSAGE) sends the MESSAGE,
%    a Java DOM, to the SOAPACTION service at the ENDPOINT.
response = callSoapService('http://www.webservicex.net/globalweather.asmx?WSDL', ...
  'http://www.webserviceX.NET/GetCitiesByCountry', m);

我收到以下回复(插入行结尾以供查看):

val =
  <?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>
      <soap:Fault>
        <faultcode>soap:Server</faultcode>
          <faultstring>System.Web.Services.Protocols.SoapException: 
          Server was unable to process request. 
          ---&gt; System.Data.SqlClient.SqlException: 
          Procedure or function 'getWCity' expects parameter '@CountryName',
          which was not supplied.
          at WebServicex.GlobalWeather.GetCitiesByCountry(String CountryName)
          --- End of inner exception stack trace ---
        </faultstring><detail />
      </soap:Fault>
    </soap:Body>
  </soap:Envelope>

我知道服务器正在响应。我可以用Python和suds这样查询它:

from suds.client import Client
url = 'http://www.webservicex.net/globalweather.asmx?WSDL'
client = Client(url)
result = client.service.GetCitiesByCountry('Australia')

我的简单问题是我做错了什么?

我还想知道如何查看createSoapMessage创建的DOM对象以及如何查看MATLAB发送和接收的xml。

1 个答案:

答案 0 :(得分:1)

正确的代码如下所示:

% createSoapMessage(NAMESPACE,METHOD,VALUES,NAMES,TYPES,STYLE) creates a SOAP message.
message = createSoapMessage( ...
  'http://www.webserviceX.NET', ...
  'GetCitiesByCountry', ...
  {'Australia'}, ...
  {'CountryName'}, ...
  {'{http://www.w3.org/2001/XMLSchema}string' }, ...
  'document')

% callSoapService(ENDPOINT,SOAPACTION,MESSAGE) sends the MESSAGE,
response = callSoapService( ...
  'http://www.webservicex.net/GlobalWeather.asmx', ...
  'http://www.webserviceX.NET/GetCitiesByCountry', ...
  message);

% parseSoapResponse Convert the response from a SOAP server into MATLAB types.
cities = parseSoapResponse(response)  

具体差异是:

  • STYLE参数是'document',而不是'rpc'
  • www.webservicex.net在资本化方面非常不一致,这很重要!
  • 端点参数以.asmx结尾并且不包含?WDSL。

我也添加了parseSoapResponse电话的示例。这也给我带来了麻烦。对于此Web服务,此调用仅返回包含所请求数据的结构。在同一主机上使用其他服务时,parseSoapResponse返回两个输出,一个好/坏结果和数据。请参阅sending SOAP request with Matlab

最后,在回答我关于在MATLAB中查看中间XML(例如message,soap消息)的补充问题时,请使用以下内容:

 javaString = message.saveXML(message.getFirstChild())

在java字符串中获取XML,然后:

 matlabString = char(javaString)

在matlab字符串中获取XML。

以下代码添加了换行符和空格,以便在多行上显示XML以帮助调试。

ms2 = regexprep(matlabString ,'>','>\n')
ms3 = regexprep(ms2,' x','\n  x')

我仍然不知道如何在浏览器中查看MATLAB中的传出和传入HTTP流量。