<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-124  評論-194  文章-0  trackbacks-0

    SOAP::Lite的Lite是說其好用,其實它的實現并不“輕量”,功能也非常強大,所以我們要用好它。

    在調用服務時,有時遇到有復雜結構或者數組時,還是有點小麻煩,下面以調用以下三個函數為例分別寫出SOAP::Lite如何組合它們的參數,其它情況也應該能迎刃而解。

    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調用及處理返回參數的代碼如下,對照相應函數及PERL中的處理來看:
    use SOAP::Lite ( +trace => all, maptype => {} );

    my $soap = SOAP::Lite
           
    -> uri('http://magic.nms.exchangebit.com/')
           
    -> on_action( subjoin '/', '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會將HASHMAP“翻譯”成結構,其中的KEY和VALUE就是結構的成員及值;另外,處理參數為結構數組及返回值也是數組的情況。組裝時要用“[]”括起來,返回值要轉換成數組指針再處理,下面是打印出來的請求、回應及函數輸出:

    請求:

    <?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>



    應答:

    <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>


    函數輸出:
    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



    補充于2007.10.15:
    另一法:

    Consume a remote procedure returning arrays with SOAP::Lite

    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 我愛佳娃 閱讀(2878) 評論(0)  編輯  收藏 所屬分類: Perl
    主站蜘蛛池模板: 亚洲精品第一国产综合野| 亚洲一区二区三区偷拍女厕| 亚洲五月激情综合图片区| 中国毛片免费观看| 亚洲色自偷自拍另类小说| 皇色在线免费视频| 亚洲av无码乱码国产精品| 永久在线观看免费视频| 亚洲国产精品无码AAA片| 国产精品99久久免费观看| 久久亚洲精品中文字幕无码| 小草在线看片免费人成视久网| 亚洲丁香色婷婷综合欲色啪| 人体大胆做受免费视频| 国产av无码专区亚洲av果冻传媒| 精品国产_亚洲人成在线高清| 亚洲综合久久综合激情久久| 99re免费在线视频| 亚洲香蕉在线观看| 国产成人免费一区二区三区| 黄页网址大全免费观看12网站| 久久久久成人片免费观看蜜芽| 成人性生免费视频| 国产大陆亚洲精品国产| 最新精品亚洲成a人在线观看| 亚洲成a人片在线不卡| 拔擦拔擦8x华人免费久久| 日本一区二区三区在线视频观看免费| 手机看片国产免费永久| 亚洲网址在线观看| 国产片免费福利片永久| aaa毛片免费观看| 亚洲综合在线观看视频| 成人午夜性A级毛片免费| 新最免费影视大全在线播放| 亚洲午夜久久影院| 免费一级成人毛片| 在线观看的免费网站无遮挡| 精品国产亚洲一区二区三区在线观看 | 国产精品久久久久免费a∨| 久久久久久亚洲av无码蜜芽|