SOAP::Lite的Lite是說(shuō)其好用,其實(shí)它的實(shí)現(xiàn)并不“輕量”,功能也非常強(qiáng)大,所以我們要用好它。
在調(diào)用服務(wù)時(shí),有時(shí)遇到有復(fù)雜結(jié)構(gòu)或者數(shù)組時(shí),還是有點(diǎn)小麻煩,下面以調(diào)用以下三個(gè)函數(shù)為例分別寫出SOAP::Lite如何組合它們的參數(shù),其它情況也應(yīng)該能迎刃而解。

public class DeviceValue
{
private String devName;
private String devIp;

public String getDevIp()
{
return devIp;
}

public void setDevIp(String devIp)
{
this.devIp = devIp;
}

public String getDevName()
{
return devName;
}

public void setDevName(String devName)
{
this.devName = devName;
}

}

public interface NotifyService
{
public int sendAlarm (DeviceValue alarm);
public String sendAlarmString (String stralarm);
public List<DeviceValue> sendAlarmArr (List<DeviceValue> arr);
}
SOAP::Lite調(diào)用及處理返回參數(shù)的代碼如下,對(duì)照相應(yīng)函數(shù)及PERL中的處理來(lái)看:
use SOAP::Lite ( +trace => all, maptype => {} );

my $soap = SOAP::Lite
-> uri('http://magic.nms.exchangebit.com/')
-> on_action( sub{ join '/', 'http://www.alfredbr.com', $_[1] })
-> proxy('http://127.0.0.1:8080/ebnms/NotifyService');

my $header = SOAP::Header->name(MyHeader => {
MyName => "Alfred"
})->uri('http://www.alfredbr.com/')->prefix('');


{# call send alarm
my @params = (
# $header,
SOAP::Data->name(x1 => goodhehe)
);
my $method = SOAP::Data->name('sendAlarmString');
# ->attr({xmlns => 'http://www.alfredbr.com/'});
my $result = $soap->call($method => @params);
print "\nsend string alarm result:\n";
if ($result->fault)
{
print $result->faultstring;
}
else
{
print $result->result;
}
print "\nn";
}

{# call send dev alarm
my @params = (
SOAP::Data->name(x1 => {devName=>"hehe", devIp=>"ip1"})
);
my $method = SOAP::Data->name('sendAlarm');
my $result = $soap->call($method => @params);
print "\nsend string alarm result:\n";
if ($result->fault)
{
print $result->faultstring;
}
else
{
print $result->result;
}
print "\n\n";
}


{# call send arr alarm
my @params = (
SOAP::Data->name(x1 => [
{devName=>"hehe1", devIp=>"ip1"},
{devName=>"hehe2", devIp=>"ip2"}])
);
my $method = SOAP::Data->name('sendAlarmArr');
my $result = $soap->call($method => @params);
print "\nsend string alarm result:\n";
if ($result->fault)
{
print $result->faultstring;
}
else
{
my @a = @{$result->result->{item}};
foreach $i (@a) {
print "ele: $i->{devName}, $i->{devIp}\n";
}
}
print "\n\n";
}
其中,尤其PERL會(huì)將HASHMAP“翻譯”成結(jié)構(gòu),其中的KEY和VALUE就是結(jié)構(gòu)的成員及值;另外,處理參數(shù)為結(jié)構(gòu)數(shù)組及返回值也是數(shù)組的情況。組裝時(shí)要用“[]”括起來(lái),返回值要轉(zhuǎn)換成數(shù)組指針再處理,下面是打印出來(lái)的請(qǐng)求、回應(yīng)及函數(shù)輸出:
請(qǐng)求:
<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:xsi="http://www.w3.or
g/2001/XMLSchema-instance" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encodi
ng/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" soap:encodingStyle="http://sch
emas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/soap/env
elope/"><soap:Body><sendAlarmArr><x1 soapenc:arrayType="xsd:anyType[2]" xsi:type
="soapenc:Array"><item><devName xsi:type="xsd:string">hehe1</devName><devIp xsi:
type="xsd:string">ip1</devIp></item><item><devName xsi:type="xsd:string">hehe2</
devName><devIp xsi:type="xsd:string">ip2</devIp></item></x1></sendAlarmArr></soa
p:Body></soap:Envelope>
應(yīng)答:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body
><sendAlarmArrResponse xmlns="http://magic.nms.exchangebit.com/"><ns2:return xml
ns:ns2="http://magic.nms.exchangebit.com/"><item><devIp>ip0</devIp><devName>name
0</devName></item><item><devIp>ip1</devIp><devName>name1</devName></item><item><
devIp>ip2</devIp><devName>name2</devName></item><item><devIp>ip3</devIp><devName
>name3</devName></item><item><devIp>ip4</devIp><devName>name4</devName></item><i
tem><devIp>ip5</devIp><devName>name5</devName></item><item><devIp>ip6</devIp><de
vName>name6</devName></item><item><devIp>ip7</devIp><devName>name7</devName></it
em><item><devIp>ip8</devIp><devName>name8</devName></item><item><devIp>ip9</devI
p><devName>name9</devName></item></ns2:return></sendAlarmArrResponse></soap:Body
></soap:Envelope>
函數(shù)輸出:
send string alarm result:
ele: name0, ip0
ele: name1, ip1
ele: name2, ip2
ele: name3, ip3
ele: name4, ip4
ele: name5, ip5
ele: name6, ip6
ele: name7, ip7
ele: name8, ip8
ele: name9, ip9
參考:
How to Call a .NET-based Web Service Using the SOAP::Lite Perl Library
補(bǔ)充于2007.10.15:
另一法:
A little more difficult now, how to parse arrays returned by web service with SOAP::Lite. We will have to use a technique similar to XPath to read the value returned in the SOAP message. The remote procedure consumed here are collInt() (returning an array of int) and collPos() (returning an array of PosCol object). The PosCol class contain just two simple int variable XPos and YPos. Let's see how to handle these cases:
# Again, no param here so we make the call
my $resultci = $client->collInt();
my $resultcp = $client->collPos();

print "<p><b>Method collInt():</b> return an array of int<br>";

# To understand this code we have to know the structure of
# the SOAP message (the response) returned by the web service
# If you take a look at the wsdl, you will find for the operation
# collInt the complex type collIntResponse, composed with an array
# of result (int) element.
# Another way is to build a classic client with your preferred language
# and catch the SOAP message returned by the web service, see
# TcpTunnelGui tool. So you know
# exactly the structure of the values.
my @intarr = $resultci->valueof('//collIntResponse/result');

# A simple foreach read the array
foreach my $intval (@intarr) {
print "elem: $intval<br>";
}

# Use the same method to parse the array of PosCol object
print "<p><b>Method collPos():</b> return an array of object <u>PosCol</u><table>";

my @poscolarr = $resultcp->valueof('//collPosResponse/result');
foreach my $pcval (@poscolarr) {
print "<tr><td>object -> </td>";

# Here, for each object you can list the key/value elements
# using usual Perl code
foreach my $keyval (keys %{$pcval}) {
print "<td>$keyval: </td><td>" . $pcval->{$keyval} . " </td>";
}
print "</tr>";
}
print "</table>";

參考了:
這里
posted on 2007-08-03 22:37
我愛(ài)佳娃 閱讀(2879)
評(píng)論(0) 編輯 收藏 所屬分類:
Perl