簡體   English   中英

使用wsdl的Perl Soap服務

[英]Perl Soap service using wsdl

我正在嘗試使用SOAP :: Lite創建SOAP服務,但是它不返回該值。 它在soap_client.pl第6行的打印中給出未初始化值$ result的錯誤使用。

<VirtualHost 192.168.1.187:80>
 ServerName perl.qlc.net
 ServerAlias www.perl.qlc.net
 DocumentRoot /web/perl.qlc.net/htdocs
 ErrorLog /var/log/httpd/perl.qlc.net_error_log
 TransferLog /var/log/httpd/perl.qlc.net_access_log
      <Directory "/web/perl.qlc.net/htdocs">
        <FilesMatch "\.mhtml$|\.pl$|\.html$">
                Options         +ExecCGI
                AddHandler      cgi-script .cgi .pl .html
                SetHandler      perl-script
                PerlHandler     ModPerl::Registry
                PerlSendHeader  On
        </FilesMatch>
      </Directory>
 DirectoryIndex index.phtml index.htm index.html index.php3 index.php
</VirtualHost>

已安裝的perl模塊:

MIME::Parser
SOAP::Lite

Apache模塊:

mod_perl

肥皂服務器:math.pl

#!/usr/bin/perl -w
use strict;
#use warnings;
use SOAP::Lite;
use SOAP::Transport::HTTP;

print "Content-type:text/xml\r\n\r\n";
#I have added above line because apache was throwing 500 error.

my $soap = SOAP::Transport::HTTP::CGI->new(
    dispatch_to => 'Arithmetic'
);
$soap->handle(); 

package Arithmetic;
sub add {
        return $_[1] + $_[2];
}
1;

肥皂客戶端:soap_client.pl

#!/usr/bin/perl -w
#use SOAP::Lite +trace =>'debug';
use SOAP::Lite;
my $result = 0;
my $client = SOAP::Lite->service("http://perl.qlc.net/math.wsdl");
print $result = $client->add(10, 20);

WSDL文件:math.wsdl

<?xml version="1.0"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://perl.qlc.net/math.wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloService" targetNamespace="http://perl.qlc.net/math.wsdl">
<message name="addRequest">
    <part name="operand1" type="xsd:float" />
    <part name="operand2" type="xsd:float" />
</message>
<message name="addResponse">
    <part name="response" type="xsd:float" />
</message>
<portType name="Hello_PortType">
    <operation name="add">
        <input message="tns:addRequest"/>
        <output message="tns:addResponse"/>
    </operation>
</portType>
<binding name="Hello_Binding" type="tns:Hello_PortType">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="add">
        <soap:operation soapAction="add"/>
        <input>
            <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:helloservice" use="encoded"/>
        </input>
        <output>
        <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:helloservice" use="encoded"/>
        </output>
    </operation>
</binding>
<service name="Hello_Service">
<documentation>WSDL File for HelloService</documentation>
<port binding="tns:Hello_Binding" name="Hello_Port">
    <soap:address location="http://perl.qlc.net/math.pl"/>
</port>
</service>
</definitions>

當我運行soap_client.pl時,出現以下錯誤:在soap_client.pl第6行中使用未初始化的值$ result在打印中。我已經嘗試了2天,沒有得到我做錯地方的任何提示。

該問題已經解決通過修改math.wsdl 改性math.wsdl

<?xml version="1.0"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="urn:Arithmetic" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloService" targetNamespace="urn:Arithmetic" xmlns:y="urn:Arithmetic">
<message name="addRequest">
    <part name="operand1" type="xsd:float" />
    <part name="operand2" type="xsd:float" />
</message>
<message name="addResponse">
    <part name="response" type="xsd:float" />
</message>
<portType name="Hello_PortType">
    <operation name="add">
        <input message="tns:addRequest"/>
        <output message="tns:addResponse"/>
    </operation>
</portType>
<binding name="Hello_Binding" type="tns:Hello_PortType">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="add">
        <soap:operation soapAction="urn:Arithmetic#add"/>
        <input>
            <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:Arithmetic" use="encoded"/>
        </input>
        <output>
        <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:Arithmetic" use="encoded"/>
        </output>
    </operation>
</binding>
<service name="Hello_Service">
<documentation>WSDL File for HelloService</documentation>
<port binding="tns:Hello_Binding" name="Hello_Port">
    <soap:address location="http://perl.qlc.net/math.pl"/>
</port>
</service>
</definitions>

您需要包括result方法以獲得結果:

$client->add(10, 20)->result;

另外,您可能應該添加一些錯誤處理,以在使用結果之前檢查命令是否成功:

my $added = $client->add(10,20);
unless (defined $added)
{
    die "Failed add: $!";
}
$result = $added->result;

另外,您是否有機會使用.net服務器? 在這種情況下Soap::Lite文檔提出了一些具體的建議 ,並建議不要使用上述格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM