Perl Tutorials - Herong's Tutorial Examples - Version 5.32, by Dr. Herong Yang
SOAP::Transport::HTTP - SOAP Server with HTTP Protocol
This section provides a tutorial example on how to use the SOAP::Transport::HTTP module to create a SOAP server with the HTTP protocol.
The SOAP::Transport::HTTP::Daemon module is very similar to the SOAP::Transport::TCP::Server module. It offers two functionalities: 1. Taking SOAP requests as a SOAP server; 2. Interacting with application modules to handle the requests as a SOAP server side API.
The SOAP::Transport::HTTP::Daemon module supports the same methods as the SOAP::Transport::TCP::Server module. See the previous chapter for details of those mothods.
Here is a sample program to show you how to create a SOAP server with HTTP as the transportation protocol and dispatch SOAP requests to application modules:
#- SoapHttpServerTrace.pl #- Copyright (c) 2002 by Dr. Herong Yang, http://www.herongyang.com/ use SOAP::Lite +trace; use SOAP::Transport::HTTP; my $daemon = SOAP::Transport::HTTP::Daemon -> new (LocalAddr=>'localhost', LocalPort=>8001, listen=>5); $daemon -> dispatch_to('Hello::hello'); print "Contact SOAP server at ", $daemon->url, "\n"; $daemon->handle();
To test the server, you need a SOAP client program that can talk to the server using HTTP protocol. Here is a sample code:
#- SoapHttpClientTrace.pl #- Copyright (c) 2002 by Dr. Herong Yang, http://www.herongyang.com/ use SOAP::Lite +trace; my $client = SOAP::Lite->new(); $client->uri('urn:Hello'); $client->proxy('http://localhost:8001'); my $som = $client->hello("Herong"); my $output = $som->result; print $output . "\n";
Notice that this client program is the same as the client program that talks to the server using TCP protocol, except the prefix code in the server URL. See the "proxy('http://localhost:8001')" function call.
Now run the server program first, then run the client program in a different command window. You should get the following output in the client program window:
SOAP::Transport::new: () SOAP::Serializer::new: () SOAP::Deserializer::new: () SOAP::Parser::new: () SOAP::Lite::new: () SOAP::Transport::HTTP::Client::new: () SOAP::Lite::call: () SOAP::Serializer::envelope: () SOAP::Serializer::envelope: hello Herong SOAP::Data::new: () SOAP::Data::new: () SOAP::Data::new: () SOAP::Data::new: () SOAP::Data::new: () SOAP::Transport::HTTP::Client::send_receive: HTTP::Request=HASH(0x25c5 a60) SOAP::Transport::HTTP::Client::send_receive: POST http://localhost:800 1/ Accept: text/xml Accept: multipart/* Content-Length: 500 Content-Type: text/xml; charset=utf-8 SOAPAction: "urn:Hello#hello" <?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-EN C="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle=" http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://sche mas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLS chema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema"><SOAP-ENV :Body><namesp1:hello xmlns:namesp1="urn:Hello"><c-gensym3 xsi:type="xs d:string">Herong</c-gensym3></namesp1:hello></SOAP-ENV:Body></SOAP-ENV :Envelope> SOAP::Transport::HTTP::Client::send_receive: HTTP::Response=HASH(0x24b 017c) SOAP::Transport::HTTP::Client::send_receive: HTTP/1.1 200 OK Date: Sat, 28 Dec 2002 17:15:01 GMT Server: libwww-perl-daemon/1.24 Content-Length: 522 Content-Type: text/xml; charset=utf-8 Client-Date: Sat, 13 Mar 2004 17:15:01 GMT Client-Peer: 127.0.0.1:8001 SOAPServer: SOAP::Lite/Perl/0.51 <?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-EN C="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle=" http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://sche mas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLS chema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema"><SOAP-ENV :Body><namesp1:helloResponse xmlns:namesp1="urn:Hello"><s-gensym3 xsi: type="xsd:string">Hello Herong</s-gensym3></namesp1:helloResonse></SOA P-ENV:Body></SOAP-ENV:Envelope> SOAP::Deserializer::deserialize: () SOAP::Parser::decode: () SOAP::SOM::new: () Hello Herong SOAP::Lite::DESTROY: () SOAP::Serializer::DESTROY: () SOAP::Data::DESTROY: () SOAP::Data::DESTROY: () SOAP::Data::DESTROY: () SOAP::Data::DESTROY: () SOAP::Data::DESTROY: () SOAP::Transport::DESTROY: () SOAP::Transport::HTTP::Client::DESTROY: () SOAP::SOM::DESTROY: () SOAP::Deserializer::DESTROY: () SOAP::Parser::DESTROY: ()
You should get the following in the server program window:
SOAP::Serializer::new: () SOAP::Deserializer::new: () SOAP::Parser::new: () SOAP::Server::new: () SOAP::Transport::HTTP::Server::new: () SOAP::Transport::HTTP::Daemon::new: () Contact SOAP server at http://localhost:8001/ SOAP::Server::handle: () SOAP::Deserializer::deserialize: () SOAP::Parser::decode: () SOAP::SOM::new: () SOAP::Data::new: () SOAP::Data::DESTROY: () (eval): Herong SOAP::Server::handle: Hello Herong SOAP::Serializer::envelope: () SOAP::Serializer::envelope: helloResponse Hello Herong SOAP::Data::new: () SOAP::Data::new: () SOAP::Data::new: () SOAP::Data::new: () SOAP::Data::new: () SOAP::SOM::DESTROY: ()
A couple of the interesting things to note here:
Table of Contents
Data Types: Values and Variables
Expressions, Operations and Simple Statements
Name Spaces and Perl Module Files
Hard References - Addresses of Memory Objects
Objects (or References) and Classes (or Packages)
Typeglob and Importing Identifiers from Other Packages
String Built-in Functions and Performance
File Handles and Data Input/Output
Open Directories and Read File Names
File System Functions and Operations
Converting Perl Script to Executable Binary
Socket Communication Over the Internet
XML::Simple Module - XML Parser and Generator
►SOAP::Lite - SOAP Server-Client Communication Module
SOAP::Transport::TCP - SOAP Server with TCP Protocol
SoapTcpClient.pl - SOAP Client Example with TCP Protocol
►SOAP::Transport::HTTP - SOAP Server with HTTP Protocol
Perl Programs as IIS Server CGI Scripts
CGI (Common Gateway Interface)
XML-RPC - Remote Procedure Call with XML and HTTP
RPC::XML - Perl Implementation of XML-RPC
Integrating Perl with Apache Web Server
CGI.pm Module for Building Web Pages