在Perl中调用WCF服务时的SOAP Action问题(SOAP Lite)

时间:2013-07-05 03:31:01

标签: wcf perl soap soaplite

使用Perl(SOAP Lite),我试图调用一个wcf Web服务,其中SOAPAction名称(ProcessMessage)与soap中的Envelope / Body / Request 元素名称(Request)不同信封体。如何指定不同的SOAPAction和Envelope-> Body-> Request元素名称?我无法更改网络服务。

以下是我尝试复制的SOAP消息示例,并且我已经高度重视了我试图单独指定/控制的两个区域。

<HttpRequest>
  <Method>POST</Method>
  <QueryString></QueryString>
  <WebHeaders>
<Content-Length>1245</Content-Length>
<Content-Type>text/xml; charset=utf-8</Content-Type>
<Accept-Encoding>gzip, deflate</Accept-Encoding>
<Expect>100-continue</Expect>
<Host>virscwapp01f1.sdpcsf.sdp.net.nz</Host>
<SOAPAction>"urn:sdp:manager:100818.ProfileScopeServiceContract.ProcessMessage"</SOAPAction>
</WebHeaders>
</HttpRequest>
 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
   <ActivityId CorrelationId="0d113c44-878d-40c4-bc3d-37a1ac9a693e" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">00000000-0000-0000-0000-000000000000</ActivityId>
    <To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://[hostname]/ProfileManager/100818/ProfileManager.svc</To>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">urn:manager:100818.ProfileScopeServiceContract.ProcessMessage</Action>
 </s:Header>
 <s:Body>
     <Request xmlns="urn:managerrequestmanager:100818" xmlns:a="urn:manager:requestmanager" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <a:Payload i:type="b:Organisation" xmlns:b="urn:manager:contract:110308">
    <b:OrganisationUnitId>OrgId</b:OrganisationUnitId>
    <b:OrganisationDescription i:nil="true"></b:OrganisationDescription>
  </a:Payload>
  <a:ProcessingInstruction xmlns:b="urn:manager:contract:common:100818">
    <b:Action>GetOrganisationById</b:Action>
    <b:Strategy>
      <b:Name i:nil="true"></b:Name>
      <b:Paging i:nil="true"></b:Paging>
      <b:SearchCriteria i:nil="true"></b:SearchCriteria>
    </b:Strategy>
    <b:Meta i:nil="true"></b:Meta>
  </a:ProcessingInstruction>
</Request>
</s:Body>
</s:Envelope>

以下是我正在使用的Perl脚本的示例

#!/usr/bin/perl

package main;
use SOAP::Lite +trace;
use LWP::UserAgent;
use HTTP::Request::Common;

# Variables
my $url = 'http://[hostname]/ProfileManager/100818/ProfileManager.svc?wsdl';
my $url_debug = 'http://localhost:11040/Service1.svc?wsdl';
my $uri = 'urn:::profilemanager:profilemanagerrequestmanager:100818';

my $soap = SOAP::Lite
-> ns( 'http://www.w3.org/2001/XMLSchema-instance', 'xsi' )
-> ns( 'urn:::profilemanager:requestmanager', 'a' )
-> ns( 'urn:::profilemanager:contract:110308', 'b'  )
-> uri($uri)
-> on_action( sub { join '.', 'urn:::profilemanager:requestmanager:100818.ProfileScopeServiceContract', $_[1] } )
-> proxy($url);


my $method = SOAP::Data->name('ProcessMessage')
->attr({xmlns => 'urn:::profilemanager:profilemanagerrequestmanager:100818'});


my @params = ( SOAP::Data->type("b:Organisation")->name("a:Payload")->value(
\SOAP::Data->value(
SOAP::Data->name("b:OrganisationUnitId")->value(""),
SOAP::Data->name("b:OrganisationDescription")->attr( { 'xsi:nil' => "true" } )  
    )
  ),
   SOAP::Data->name("a:ProcessingInstruction")->value(
   \SOAP::Data->value(
     SOAP::Data->name("b:Action")->value("GetOrganisationById"),
SOAP::Data->name("b:Strategy")->value(
    \SOAP::Data->value(
          SOAP::Data->name("b:Name")->attr( { 'xsi:nil' => "true" } ),
     SOAP::Data->name("b:Paging")->attr( { 'xsi:nil' => "true" } ),
     SOAP::Data->name("b:SearchCriteria")->attr( { 'xsi:nil' => "true" } )
     )
      ),
      SOAP::Data->name("b:Meta")->attr( { 'xsi:nil' => "true" } )
)
)
);

print $soap->call($method => @params)->result; 

不要过多关注我的命名空间,因为我删除了部分命名空间。

1 个答案:

答案 0 :(得分:0)

我必须这样做以使SOAP :: Lite与WCF协同工作:

my $serviceInterface = 'IMyWcfServiceInterface';

# Setup Network Connection
my $service = SOAP::Lite
    ->ns( $namespace, $namespacePrefix )
    ->proxy( $server, credentials => [ "$host:$port", $realm, $username => $password ] )
    ->on_action( sub {
        my $action = sprintf( '%s$serviceInterface/%s', @_ );
        print( "action: '$action'\n" );
        return $action;
    } );

my $response = $service->MyServiceFunction()

在您的情况下,您似乎希望操作为:urn:sdp:manager:100818.ProfileScopeServiceContract.ProcessMessage,因此您应该可以使用:

...
    ->on_action( 'urn:sdp:manager:100818.ProfileScopeServiceContract.ProcessMessage' )
...

Perl pom documentation

on_action(callback)

    $client->on_action(sub { qq("$_[0]") });
  

当传输对象为基于HTTP的SOAPAction标头设置时触发   呼叫。默认设置是将标头设置为字符串uri#method,其中URI为
  由前面描述的uri方法设置的值,方法是方法的名称
  被召唤。调用时,引用的例程(或闭包,如果指定为   示例)按顺序给出了两个参数uri和method。

     

.NET Web服务通常期望/作为uri和方法的分隔符。要更改SOAP :: Lite    要将uri / method用作SOAPAction标头的行为,请使用以下代码:

    $client->on_action( sub { join '/', @_ } );