4

我正在尝试使用 perl 和 SOAP Lite 来使用 .Net Web 服务。

当我在 .Net 客户端中使用 Web 服务时,它会向 .asmx 端点发布以下内容:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tns="http://mysoapnamespace.com/"
xmlns:types="http://mysoapnamespace.com/encodedTypes">
  <soap:Body>
    <tns:HellowWorld  />
  </soap:Body>
</soap:Envelope>

如何使用 SOAP Lite 生成相同的请求?我浏览了各种 SOAP Lite 文档和文章,但都没有运气。到目前为止,我有以下内容:

#!/usr/bin/perl
use SOAP::Lite 'trace', 'debug' ;

$api_ns = "https://mysoapnamespace.com";
$api_url = "http://mysoapnamespace/api.asmx";
$action = "HelloWorld";

  my $soap = SOAP::Lite 
   -> readable(1)
   -> uri($api_ns)
   -> proxy($api_url)
   -> on_action(sub { return "\"$action\"" }) ;


return $soap->HellowWorld();

这会生成这个不正确的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <HellowWorld xmlns="http://mysoapnamespace.com" xsi:nil="true" />
      </soap:Body>
</soap:Envelope>

更新:

当我使用 fiddler 将第一个 xml 发布到我的服务时,它会返回我的“Hello World”结果。当我发布第二个时,我得到以下信息:

<?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:Client</faultcode><faultstring>System.Web.Services.Protocols.SoapException: Server was unable to read request. ---&gt; System.InvalidOperationException: There is an error in XML document (9, 6). ---&gt; System.InvalidOperationException: &lt;HellowWorld xmlns='http://mysoapnamespace.com'&gt; was not expected.
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read21_HellowWorld()
   at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer28.Deserialize(XmlSerializationReader reader)
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
   at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()
   --- End of inner exception stack trace ---
   at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope>
4

2 回答 2

4

发现了问题 - 命名空间上的斜杠。.Net 只是为您解决这些问题,但需要在 Perl 中明确设置。此外,还了解了如何使用 ns() 函数添加名称空间。

这将生成正确的 XML。

#!/usr/bin/perl
use SOAP::Lite 'trace', 'debug' ;

$api_ns = "https://mysoapnamespace.com/";
$api_url = "http://mysoapnamespace/api.asmx";
$action = "HelloWorld";

  my $soap = SOAP::Lite 
   -> readable(1)
   -> ns($api_types,'types')
   -> ns($api_ns,'tns')
   -> proxy($api_url)
   -> on_action(sub { return "\"$action\"" }) ;


return $soap->HellowWorld();

这个链接对找出 SOAP::Lite 非常有帮助 - http://kobesearch.cpan.org/htdocs/SOAP-Lite/SOAP/Lite.pm.html

于 2010-07-19T21:17:41.017 回答
0

我必须执行以下操作才能使其正常工作(在我的 $soap... 行之后添加此行):

$soap->ns('http://schemas.xmlsoap.org/soap/envelope/',"s");

我希望这可以节省一些时间...花了一段时间才弄清楚... :-)

顺便说一句:我正在使用 .Net 4.5 WCF 和用于 Web 服务服务器的 Windows 服务和带有 SOAP::Lite V 1.08 的 Perl (activestate) V5.16.3。

于 2014-01-07T19:52:19.353 回答