如何在Soap:Lite中将命名空间前缀明确更改为“ xmlns”

时间:2019-06-12 23:55:11

标签: c# perl soap soaplite

我正在c#中设置一个Web服务,他们正在使用perl Soap :: Lite调用它。我正在尝试让该程序删除名称空间prefox,而是在action标签中将完整名称空间定义为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服务中显示为空,因为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

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

  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
        }
    }

}

1 个答案:

答案 0 :(得分:1)

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

<块引用>

register_ns : register_ns 子程序允许用户注册一个 带有 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