首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Soap:Lite中将命名空间前缀显式更改为"xmlns“

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

Stack Overflow用户
提问于 2019-06-13 07:55:11
回答 1查看 453关注 0票数 2

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

代码语言:javascript
复制
<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>

生成上述请求的代码如下所示

代码语言:javascript
复制
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>

我希望请求看起来像这样

代码语言:javascript
复制
<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>

或者像这样

代码语言:javascript
复制
<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中显式地定义元素标签。

代码语言:javascript
复制
$soap->call('myMethod',
     SOAP::Data->name('name')->value( 'First Cycle' ),
    SOAP::Data->name('desc')->value('The Description') );

请帮助(-:

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

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

代码语言:javascript
复制
  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
        }
    }

}
EN

回答 1

Stack Overflow用户

发布于 2021-07-29 11:53:01

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

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

根据您的代码需要添加这些行(对于示例,注册一些名称空间参数)

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56571728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档