perl实现的最简单的SOAP服务端和客户端代码(不需要web服务器),供大家学习参考。
服务端代码(不需要web服务器,直接运行即可):
use SOAP::Transport::HTTP;
#提供给客户端调用的函数
sub runBiz()
{
my ($a,$packageName,$bizDataXML) = (@_);
print "receive[$packageName,$bizDataXML]";
# 返回给客户端的值
return SOAP::Data->name(result => "soap server receive[$packageName,$bizDataXML]");
}
#启动服务端
my $daemon = SOAP::Transport::HTTP::Daemon
-> new (LocalAddr => 'localhost',LocalPort => 9080)
# 把函数加载到服务里 dispatch_to('/Your/Path/To/Deployed/Modules', 'Module::Name', 'Module::method')
#dispatch_to(
# 'PATH/', # dynamic: load anything from there, any module, any method
# 'MODULE', # static: any method from this module
# 'MODULE::method', # static: specified method from this module
# 'method', # static: specified method from main::
# );
-> dispatch_to("runBiz")
;
print "Contact to SOAP server at ", $daemon->url, "n";
# 启动监听
$daemon->handle;
客户端代码:
use SOAP::Lite;
my $bizDataXML='<?xml version="1.0" encoding="GB2312"?>
<root>
<data>
<WGINFOR>XXXXXXXX</WGINFOR>
<TIME>2010-09-19 18:17:20</TIME>
';
$soap_client = SOAP::Lite
# -> uri('runBiz')
-> proxy("http://localhost:9080/");
#设置用户名密码
#eval { $soap_client->transport->http_request->header
#(
# 'Authorization' =>
# 'Basic ' . MIME::Base64::encode("$sUID:$sPWD", '')
#); };
$soap_response = $soap_client->runBiz
(
SOAP::Data->name(packageName=>"ISS_KM"), #参数1
SOAP::Data->name(bizDataXML => $bizDataXML) #参数2
);
#打印返回值
print $soap_response->result,"n";
测试环境:winXP perl v5.6.1 built for MSWin32-x86-multi-thread
(图1)
(图2)