简体   繁体   English

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

[英]How to change Namespace Prefix explicitly to “xmlns” in Soap:Lite

I'm setting up a Web Service in c# and they are calling it using perl Soap::Lite.我在 c# 中设置了一个 Web 服务,他们使用 perl Soap::Lite 调用它。 I am trying to get the program to remove the namespace prefox, and instead, define the full namespace as an xmlns attribute in the action tag.我试图让程序删除命名空间前缀,而是将完整命名空间定义为操作标记中的 xmlns 属性。 The request generated looks like this生成的请求如下所示

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

The code that generated the request above is this上面生成请求的代码是这样的

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";

The parameters in the above show null in the web service because C# parses the parameters as being in different namespaces.上面的参数在 Web 服务中显示为 null,因为 C# 将参数解析为位于不同的命名空间中。 instead of the prefix, I'd like the action to look like this而不是前缀,我希望动作看起来像这样

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

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

instead of the service description.而不是服务描述。 It works but the paramters are changes to something like this <c-sysgen3 xsi:type="s:string"><c-sysgen3>它有效,但参数更改为类似<c-sysgen3 xsi:type="s:string"><c-sysgen3>

I want the request to look like this我希望请求看起来像这样

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

or like this或者像这样

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

I should mention I am not allowed to explicitly define the element tags in perl eg我应该提到我不允许在 perl 中明确定义元素标签,例如

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

Please Help (-:请帮忙 (-:

Edit: My version of perl is 5.28.1 and the version of SOAP::Lite is 1.27 Im also using .net framework 4.5编辑:我的 perl 版本是 5.28.1,SOAP::Lite 版本是 1.27 我也在使用 .net framework 4.5

Edit: I should probably add my web service implementation编辑:我可能应该添加我的 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
        }
    }

}

As per your requirement you can use register_ns method provided by SOAP::Serializer class根据您的要求,您可以使用 SOAP::Serializer 类提供的 register_ns 方法

register_ns : The register_ns subroutine allows users to register a global namespace with the SOAP Envelope. register_ns : register_ns 子例程允许用户使用 SOAP Envelope 注册全局名称空间。 The first parameter is the namespace, the second parameter to this subroutine is an optional prefix.第一个参数是命名空间,这个子程序的第二个参数是一个可选的前缀。 If a prefix is not provided, one will be generated automatically for you.如果未提供前缀,将自动为您生成一个。 All namespaces registered with the serializer get declared in the <soap:Envelope /> element.使用序列化程序注册的所有命名空间都在 <soap:Envelope /> 元素中声明。

As per your code need to add these line (for sample, register few namespace parameter)根据您的代码需要添加这些行(例如,注册一些命名空间参数)

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');
#====#

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM