Perl的SOAP Web服务调用使用XML

问题描述:

我通过使用Perl与SOAP::Lite模块调用SOAP Web服务如下:Perl的SOAP Web服务调用使用XML

my $soap = SOAP::Lite->on_action(sub { join '/', @_ }) 
     ->readable(1) 
     ->uri($uri) 
     ->proxy($proxy) 
     ->ns($ns); 

$soap->call(
     'method' => (SOAP::Data->name(...)) 
); 

有没有用,而不是在SOAPUI显示XML定义致电客户网络服务的任何方式写SOAP::Data等?如果有选择这样做会更容易。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:int="xxx"> 
    <soapenv:Header/> 
    <soapenv:Body> 
    <int:method> 
     <!--Optional:--> 
     <int:userName>?</int:userName> 
     <!--Optional:--> 
     <int:password>?</int:password> 
     <!--Optional:--> 
     ... 
    </int:method> 
    </soapenv:Body> 
</soapenv:Envelope> 

例如,下面有可能吗?

my $xml_string = qq((<soapenv:Envelope 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:int="xxx"> 
     <soapenv:Header/> 
     <soapenv:Body> 
      <int:method> 
      <!--Optional:--> 
      <int:userName>$username</int:userName> 
      <!--Optional:--> 
      <int:password>$password</int:password> 
      <!--Optional:--> 
      ............ 
      ........... 
      </int:method> 
     </soapenv:Body> 
    </soapenv:Envelope> 
)); 
$xml_string->process; 
+0

是的,这是可能的。我稍后会写一个答案。 – simbabque

+0

您是否有WSDL文件或其他任何描述SOAP端点的文件?如果您有WSDL,请访问http://*.com/a/11991202/1331451帮助。 – simbabque

您可以使用heredoc风格并提交您的整个肥皂数据作为普通的发布请求。

如果来自远程主机的SOAP响应时间过长,并且您必须同时运行其他代码,则还可以使用非阻塞样式。

SOAP协议的设计者对这种简化的使用并不满意,因为它太简单了。

use strict; 
use warnings; 
use Mojo::UserAgent; 
my $ua = Mojo::UserAgent->new; 

my $username = "MyUsername"; 
my $password = "MyPassword"; 

my $hash; 
$hash->{variable} = "SomeText"; 

my $SOAP_request = <<"END_MESSAGE"; 
<soapenv:Envelope 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:int="xxx"> 
     <soapenv:Header/> 
     <soapenv:Body> 
      <int:method> 
      <!--Optional:--> 
      <int:userName>$username</int:userName> 
      <!--Optional:--> 
      <int:password>$password</int:password> 
      <!--Optional:--> 
      <int:variable>@{[$hash->{variable}]} 
      ............ 
      ........... 
      </int:method> 
     </soapenv:Body> 
</soapenv:Envelope> 
END_MESSAGE 

阻断

my $res = $ua->post('http:///www.example.com' => $SOAP_request); 
print $res->body; 

非阻断

$ua->post('http://www.example.com' => $SOAP_request => sub { 
    my ($c, $tx) = @_; 
    print $tx->res->body;    
}); 

读取响应

use SOAP::Lite; 
my $som = SOAP::Deserializer->deserialize($tx->res->text); 

# Simple xPath navigation through tags 
$som->valueof('//Response//SomeInformation//Details');