使用Perl和SOAP Lite消耗.Net Web服务

时间:2010-07-19 20:37:22

标签: perl soap soap-client soaplite

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

2 个答案:

答案 0 :(得分: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

非常有帮助

答案 1 :(得分:0)

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

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

我希望这能节省一些时间......需要一段时间才能弄清楚......: - )

BTW:我正在使用.Net 4.5 WCF与Web服务的Windows服务和带有SOAP :: Lite V 1.08的Perl(activestate)V5.16.3。