2

我在 c# 中设置了一个 Web 服务,他们使用 perl Soap::Lite 调用它。我试图让程序删除命名空间前缀,而是将完整的命名空间定义为操作标记中的 xmlns 属性。生成的请求如下所示

<soap:Envelope
    soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
    xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
    xmlns:s="http://www.w3.org/2001/XMLSchema"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
    xmlns:tns="https://mynamespace.org"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Body>
    <tns:myMethod>
      <name xsi:type="s:string">First Cycle</name>
      <desc xsi:type="s:string"> The Description</desc>
    </tns:myMethod>
  </soap:Body>
</soap:Envelope>

生成上述请求的代码是这样的

use strict ;
use warnings ;

use SOAP::Lite +trace => [ transport => sub { print $_[0]->as_string } ];;

my $ns = "https://mynamespace.org";

my $HOST = "http://localhost:54387/WebService.asmx";

my $wsdl = "http://localhost:54387/WebService.asmx?wsdl";

my $soap = SOAP::Lite->service($wsdl);
$soap->default_ns($ns);

 my $som = $soap->myMethod(
     'First Cycle', 'The Description'
);

 die $som->fault->{ faultstring } if ($som->fault);
 print $som->result, "\n";

上面的参数在 Web 服务中显示为 null,因为 C# 将参数解析为位于不同的命名空间中。而不是前缀,我希望动作看起来像这样

<myMethod xmlns="https://mynamespace.org">

我试过使用 my $soap = SOAP::Lite->new( proxy => $HOST);

而不是服务描述。它有效,但参数是这样的变化<c-sysgen3 xsi:type="s:string"><c-sysgen3>

我希望请求看起来像这样

<soap:Envelope
    soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
    xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
    xmlns:s="http://www.w3.org/2001/XMLSchema"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
    xmlns:tns="https://mynamespace.org"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Body>
    <myMethod xmlns="https://mynamespace.org">
      <name xsi:type="s:string">First Cycle</name>
      <desc xsi:type="s:string"> The Description</desc>
    </myMethod>
  </soap:Body>
</soap:Envelope>

或者像这样

<soap:Envelope
    soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
    xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
    xmlns:s="http://www.w3.org/2001/XMLSchema"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
    xmlns:tns="https://mynamespace.org"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Body>
    <tns:myMethod>
      <tns:name xsi:type="s:string">First Cycle</tns:name>
      <tns:desc xsi:type="s:string"> The Description</tns:desc>
    </tns:myMethod>
  </soap:Body>
</soap:Envelope>

我应该提一下,我不允许在 perl 中显式定义元素标签,例如

$soap->call('myMethod',
     SOAP::Data->name('name')->value( 'First Cycle' ),
    SOAP::Data->name('desc')->value('The Description') );

请帮忙 (-:

编辑:我的 perl 版本是 5.28.1,SOAP::Lite 的版本是 1.27 我也使用 .net framework 4.5

编辑:我可能应该添加我的网络服务实现

  namespace WebService
{ 

  [WebService(Namespace = "https://mynamespace.org")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   [System.ComponentModel.ToolboxItem(false)]
    public class SoapService : System.Web.Services.WebService
    {

        [WebMethod]
        public string myMethod(string name, string desc)
        {
            // Implementation here
        }
    }

}
4

1 回答 1

1

根据您的要求,您可以使用 SOAP::Serializer 类提供的 register_ns 方法

register_ns : register_ns 子例程允许用户向 SOAP Envelope 注册一个全局名称空间。第一个参数是名称空间,该子例程的第二个参数是可选前缀。如果没有提供前缀,则会自动为您生成一个。使用序列化程序注册的所有命名空间都在 <soap:Envelope /> 元素中声明。

根据您的代码需要添加这些行(例如,注册几个命名空间参数)

my $ns = "https://mynamespace.org";
my $HOST = "http://localhost:54387/WebService.asmx";
my $wsdl = "http://localhost:54387/WebService.asmx?wsdl";

my $soap = SOAP::Lite->service($wsdl);
#============#
$soap->serializer->register_ns('http://microsoft.com/wsdl/mime/textMatching/','tm');
$soap->serializer->register_ns('http://schemas.xmlsoap.org/wsdl/soap12/','soap12');
$soap->serializer->register_ns('urn:com:ssn:schema:service:v2.7:BasicTypes.xsd','urn3');
$soap->default_ns($ns);
# Gets or sets the prefix that labels the SOAP envelope namespace. This defaults to SOAP-ENV.
$soap->envprefix('SOAP-ENV');
#====#

请检查https://metacpan.org/dist/SOAP-Lite/view/lib/SOAP/Serializer.pod

于 2021-07-29T11:53:01.223 回答