如何在Perl中访问简单的SOAP服务
我目前正在使用SOAP :: Lite涉及perl和SOAP.
我有一个简单的SOAP服务器似乎运行正常: #!perl -w use SOAP::Transport::HTTP; use Demo; # don't want to die on 'Broken pipe' or Ctrl-C $SIG{PIPE} = $SIG{INT} = 'IGNORE'; my $daemon = SOAP::Transport::HTTP::Daemon -> new (LocalPort => 801) -> dispatch_to('/home/soaplite/modules') ; print "Contact to SOAP server at ",$daemon->url,"n"; $daemon->handle; 它包含一个名为Demo的小类,它只检索系统总内存: Demo.py #!/usr/bin/perl use Sys::MemInfo qw(totalmem freemem totalswap); print "total memory: ".(&totalmem / 1024)."n"; 我有一个下面用PERL编写的SOAP客户端的例子,虽然我不确定如何与服务器通信(因为我在这里跟随的tutorial是切线,例如从客户端检索Demo.py类的结果: #!perl -w use SOAP::Lite; # Frontier http://www.userland.com/ $s = SOAP::Lite -> uri('/examples') -> on_action(sub { sprintf '"%s"',shift }) -> proxy('http://superhonker.userland.com/') ; print $s->getStateName(SOAP::Data->name(statenum => 25))->result; 任何帮助将不胜感激 :) 解决方法
对于服务器脚本,dispatch_to方法获取要加载的包的路径以及包本身的名称.如果传递第三个参数,它将限制服务器可见的方法的名称. (例如,2个方法称为内存和时间,将Demo :: time作为第3个参数传递给客户端服务将使内存不可见.)
文件server.pl my $daemon = SOAP::Transport::HTTP::Daemon -> new (LocalPort => 801) -> dispatch_to('/home/soaplite/modules','Demo') ; 您的Demo包应该是包含返回值的方法的包.我无法在我的系统上编译Sys :: MemInfo,所以我只使用了localtime.我不确定你为什么命名你的包Demo.py,但是Perl包必须有扩展名pm,否则它们将无法正确加载. 文件演示.pm #!/usr/bin/perl package Demo; #use Sys::MemInfo qw(totalmem freemem totalswap); sub memory { #print "total memory: ".(&totalmem / 1024)."n"; return "Can't load Sys::MemInfo,sorry"; } sub time { my $time = localtime; return $time; } 1; 对于客户端代码,必须正确指定2个重要部分才能工作,代理和uri.代理是soap Web服务的url路径.由于您将服务器脚本作为守护程序进程运行,因此您的路径只是网站的URL.我的电脑没有网址,所以我使用了http:// localhost:801 /. 801是您在上面指定的端口.如果您在不同的Web服务器(例如Apache)中作为cgi脚本运行,那么您需要指定要调用的cgi脚本(例如http://localhost/cgi-bin/server.pl,更改包在server.pl到SOAP :: Transport :: HTTP :: CGI. uri可能是最令人困惑的,但它是Web服务返回的xml文件的命名空间.开启跟踪=> ‘debug’查看Web服务返回的xml文件. uri应该只是服务器的名称.即使你切换端口或cgi调度方法,这个uri保持不变. 文件test.pl #!perl -w use SOAP::Lite +trace => 'debug'; # Frontier http://www.userland.com/ $s = SOAP::Lite->new(proxy => 'http://superhonker.userland.com:801/',uri => 'http://superhonker.userland.com/'); #might be http://www.userland.com/ #but I could not test sub-domains print $s->time()->result; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |