<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
    主站蜘蛛池模板: 国产精品亚洲片在线花蝴蝶| 亚洲一区在线免费观看| 亚洲综合国产成人丁香五月激情| 亚洲中文字幕无码日韩| 四只虎免费永久观看| 国产精品69白浆在线观看免费| 免费视频精品一区二区三区| 免费夜色污私人影院网站| 亚洲国产精品成人午夜在线观看| 亚洲日本乱码一区二区在线二产线 | 精品四虎免费观看国产高清午夜| 看成年女人免费午夜视频| 精品亚洲456在线播放| 亚洲精品国产啊女成拍色拍| 亚洲第一极品精品无码久久| 国产成人精品久久亚洲高清不卡 | 国产精品嫩草影院免费| 岛国片在线免费观看| 97视频热人人精品免费| 18禁止观看免费私人影院| 免费无码一区二区三区| 亚洲精品免费观看| 久久久久国产精品免费免费不卡| 你懂的在线免费观看| 在线视频网址免费播放| 国产精品免费看久久久香蕉| 一级毛片人与动免费观看| 免费看一级高潮毛片| 免费观看四虎精品成人| 一级视频在线免费观看| 曰韩无码AV片免费播放不卡| 污污免费在线观看| 一级做a爰片久久毛片免费陪| 一区二区免费国产在线观看| 四虎精品成人免费视频| 国产免费福利体检区久久| 久久久久久毛片免费看 | 亚洲欧洲国产精品香蕉网| 亚洲老妈激情一区二区三区| 亚洲精品自在在线观看| 久久亚洲伊人中字综合精品|