1

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

% 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。

4

1 回答 1

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 参数是“文档”,而不是“rpc”
  • www.webservicex.net 的资本化方式非常不一致,这很重要!
  • 端点参数以 .asmx 结尾,不包括 ?WDSL。

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

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

 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 流量,就像在浏览器中一样。

于 2012-08-15T10:19:40.570 回答