This section provides a tutorial example on how to use the SOAP::Data class to build the request parameter element with correct element name and a namespace. My first SOAP::Lite program works now with the GetSpeech Web service.
In the previous tutorial, I confirmed that the parameter name "c-gensym3"
generated with SOAP::Lite default setting is the root cause of the exception of
"System.Web.Services.Protocols.SoapException: Server was unable to process request.
---> System.NullReferenceException: Object reference not set to an instance of an object."
What SOAP::Lite function should I use to fix this issue?
Looking at the ActivePerl SOAP::Lite manual again, I found this class:
SOAP::Data
You can use this class if you want to specify a value, a name,
atype, a uri or attributes for SOAP elements (use value(), name(),
type(), uri() and attr() methods correspondingly). For example,
SOAP::Data->name('abc')->value(123) will be serialized into
123, as well as will SOAP::Data->name(abc => 123). Each
of them (except the value() method) can accept a value as the
second parameter. All methods return the current value if you call
them without parameters. The return the object otherwise, so you
can stack them.
Based on the above description I revised my test program as
GetSpeech_Parameter_Name.pl:
#- GetSpeech_Parameter_Name.pl
#- Copyright (c) 2009 by Dr. Herong Yang, herongyang.com
#- All rights reserved
#
use SOAP::Lite +trace;
my $client = SOAP::Lite->new();
$client->proxy('http://www.xmlme.com/WSShakespeare.asmx');
#- Fixing the SOAPAction header line - join uri and method name with /
$client->uri('http://xmlme.com/WebServices');
$client->on_action(sub { join '/', @_ });
#- Building the parameter element with name and namespace
my $parameter = SOAP::Data
->uri('http://xmlme.com/WebServices')
->name(Request)
->value("To be, or not to be");
#- Calling the server
my $som = $client->GetSpeech($parameter);
my $output = $som->result;
print $output . "\n";
Here is what I did in this program:
The string parameter is replaced with a SOAP::Date object, $parameter,
when calling the Web service method GetSpeech().
A URI string is added with uri() as the namespace for $parameter.
"Request" is added with name() as the name for $parameter.
"To be, or not to be" is added with value() as the value for $parameter.
When calling GetSpeech($parameter), $parameter will be serialized
as the parameter element.
When executing GetSpeech_Parameter_Name.pl, I got this:
\herong>GetSpeech_Parameter_Name.pl
...
POST http://www.xmlme.com/WSShakespeare.asmx
Accept: text/xml
Accept: multipart/*
Content-Length: 597
Content-Type: text/xml; charset=utf-8
SOAPAction: http://xmlme.com/WebServices/GetSpeech
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/1999/XMLSchema"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<namesp1:GetSpeech xmlns:namesp1="http://xmlme.com/WebServices">
<namesp2:Request xsi:type="xsd:string"
xmlns:namesp2="http://xmlme.com/WebServices">
To be, or not to be
</namesp2:Request>
</namesp1:GetSpeech>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
...
HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Connection: close
Date: ... 2009
Server: Microsoft-IIS/6.0
Content-Length: 1942
Content-Type: text/xml; charset=utf-8
Client-Date: ... 2009
Client-Peer: ...
Client-Response-Num: 1
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetSpeechResponse xmlns="http://xmlme.com/WebServices">
<GetSpeechResult><SPEECH><PLAY>HAMLET</PLAY>
<SPEAKER>HAMLET</SPEAKER>To be, or not to be:
that is the question: Whether 'tis nobler in the mind
...</SPEECH>
</GetSpeechResult>
</GetSpeechResponse>
</soap:Body>
</soap:Envelope>
...
Bingo! I got my first SOAP::Lite program working with GetSpeech Web service
provided by xmlme.com.