This section provides a tutorial example on how to use SocketRequestResponse.pl to send a SOAP 1.1 request for the GetSpeech Web service provided by www.xmlme.com. The response of GetSpeech received looks very nice.
In the previous tutorial, I discovered a code bug in my socket test program, SocketRequestResponse.pl.
The bug is that the request file is opened in non-binary mode and the read() functions converts
\r\n to \n resulting less bytes used in the SOAP request message.
I modified SocketRequestResponse.pl to open the request file in binary mode:
#- SocketRequestResponseBinary.pl
#- Copyright (c) 2009 by Dr. Herong Yang, herongyang.com
#- All rights reserved
#
use Socket;
($host, $port, $in, $out) = @ARGV;
#- Reading the request
open(IN, "< $in");
binmode(IN);
read(IN, $req, (-s $in));
close(IN);
#- Connecting
socket(SOCK,PF_INET,SOCK_STREAM,getprotobyname('tcp'));
connect(SOCK, sockaddr_in($port, inet_aton($host)));
select(SOCK); $| = 1; select(STDOUT);
#- Sending the request
print SOCK $req;
#- Receiving the response
open(OUT, "> $out");
while ($line=<SOCK>) {
print OUT $line;
}
close(OUT);
#- Closing connection
close(SOCK);
exit;
Here is the test result after fixing the bug:
\herong>SocketRequestResponseBinary.pl www.xmlme.com 80
soap_1_1_GetSpeech.req soap_1_1.res
\herong>type soap_1_1.res
HTTP/1.1 200 OK
Connection: close
Date: ... 2009
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 1942
<?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 to
...</SPEECH>
</GetSpeechResult>
</GetSpeechResponse>
</soap:Body>
</soap:Envelope>
Cool. My first SOAP 1.1 test with SocketRequestResponseBinary.pl worked nicely.
Some notes on the SOAP response:
www.xmlme.com is running Microsoft IIS 6.0 as the Web server.
The GetSpeech Web service returns result in the GetSpeechResult element
of the SOAP response XML message.
The result itself is an XML message with < and > replaced as escape
sequences.
The SOAP response message received as a single line without any line breaks.
I reformatted it before inludeding it here.