加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 06:16:22 所属栏目:大数据 来源:网络整理
导读:我正在尝试使用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:
我正在尝试使用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,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 :: Lite – http://kobesearch.cpan.org/htdocs/SOAP-Lite/SOAP/Lite.pm.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读