使用XML的Perl Soap Web服务调用 [英] Perl SOAP Web Service Call With XML
本文介绍了使用XML的Perl Soap Web服务调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用带有SOAP::Lite
模块的Perl调用SOAPWeb服务,如下所示:
my $soap = SOAP::Lite->on_action( sub { join '/', @_ } )
->readable( 1 )
->uri( $uri )
->proxy( $proxy )
->ns( $ns );
$soap->call(
'method' => ( SOAP::Data->name( ... ) )
);
有没有办法不写SOAP::Data
等,而是用SOAPUI上显示的XML定义来调用客户Web服务?如果有这样做的选择,事情会更容易。
<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;
推荐答案
您可以使用heredoc样式并将整个SOAP数据作为普通POST请求提交。
如果来自远程主机的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');
这篇关于使用XML的Perl Soap Web服务调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文