使用Perl和SOAP Lite来使用.Net Web服务
问题描述:
我想使用perl和SOAP Lite来使用.Net Web服务。使用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精简版相同的请求?我已经通过各种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>
更新:
当我使用招后的第一个XML到我的服务,它返回我的“你好世界“的结果。当我发布第二个时,我得到以下内容:
<?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. ---> System.InvalidOperationException: There is an error in XML document (9, 6). ---> System.InvalidOperationException: <HellowWorld xmlns='http://mysoapnamespace.com'> 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>
答
发现问题 - 命名空间上的结尾斜杠。 .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 ::精简版非常有帮助 - http://kobesearch.cpan.org/htdocs/SOAP-Lite/SOAP/Lite.pm.html
答
我不得不做以下才能正常的工作(我的$肥皂......行之后添加此行):
$soap->ns('http://schemas.xmlsoap.org/soap/envelope/',"s");
我希望这会节约一些时间......我花了一段时间才能得到它想出... :-)
BTW:我使用.NET 4.5 WCF与窗口服务用于Web服务服务器和P erl(activestate)V5.16.3与SOAP :: Lite V 1.08。
第二个有什么问题?除了额外的xsi:nil属性外,它们看起来是相同的。 – 2010-07-19 21:03:06
不确定,更新的问题与我回来的错误。 – brendan 2010-07-19 21:09:06