2010-07-19 69 views
4

我想使用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. ---&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> 
+0

第二個有什麼問題?除了額外的xsi:nil屬性外,它們看起來是相同的。 – 2010-07-19 21:03:06

+0

不確定,更新的問題與我回來的錯誤。 – brendan 2010-07-19 21:09:06

回答

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 ::精簡版非常有幫助 - http://kobesearch.cpan.org/htdocs/SOAP-Lite/SOAP/Lite.pm.html

+0

順便說一句,任何CPAN模塊文檔的規範鏈接總是'http://search.cpan.org/perldoc? ',或者直接訪問http://search.cpan.org並在搜索字段中輸入模塊名稱。如果你真的很懶,只需google「cpan 」,你就可以在99%的時間內得到正確的結果。 – Ether 2010-07-19 22:10:19

+0

很酷,謝謝! - 絕對不是這裏的Perl專家 – brendan 2010-07-20 01:09:29

0

我不得不做以下才能正常的工作(我的$肥皂......行之後添加此行):

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