【问题标题】:Consuming .Net Web Service using Perl and SOAP Lite使用 Perl 和 SOAP Lite 使用 .Net Web 服务
【发布时间】:2010-07-19 20:37:21
【问题描述】:

我正在尝试使用 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>

【问题讨论】:

  • 第二个有什么问题?除了额外的 xsi:nil 属性外,它们看起来是等价的。
  • 不太确定,更新了我回来的错误问题。

标签: perl soap soap-client soaplite


【解决方案1】:

发现问题 - 命名空间上的尾部斜杠。 .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 非常有帮助

【讨论】:

  • 顺便说一句,任何 CPAN 模块文档的规范链接始终是 http://search.cpan.org/perldoc?&lt;module name&gt;,或者只需转到 search.cpan.org 并在搜索字段中输入模块名称。如果您真的很懒,只需 google “cpan ”,99% 的时间都会得到正确的结果。
【解决方案2】:

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

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

我希望这会节省一些时间...花了一段时间才弄明白... :-)

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

【讨论】:

    猜你喜欢
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    相关资源
    最近更新 更多