The Mobile Developer
by Eric Giguère
SOAP: Invoking Object Methods With HTML
If you wanted to perform some kind of remote procedure call-- I'm using the term in a general sense to mean invoking a method in an object
residing on another machine -- what framework would you use? IIOP (CORBA)? DCOM? Java RMI?
Choosing any of these ties you to a particular architecture and can present problems when your try to move clients outside the corporate firewall.
An interesting alternative to these technologies is SOAP, developed by several companies including DevelopMentor, Microsoft, IBM/Lotus, and
UserLand Software. SOAP stands for Simple Object Access Protocol and uses both XML and HTTP to provide an interoperable way to
invoke methods on remote computers.
The way SOAP works is very simple. First, encode a method invocation request as an XML document. Second, send it to the appropriate server using
HTTP. Third, wait for the response from the server, also encoded as an XML document. Repeat as necessary.
Some examples would help. Here's an example of a SOAP request taken straight from the SOAP
Specification:
POST /StockQuote HTTP/1.1
Host: www.stockquoteserver.com
Content-Type: text/xml;
charset="utf-8"
Content-Length: nnnn
SOAPAction: "Some-URI"
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<m:GetLastTradePrice xmlns:m="Some-URI">
<symbol>DIS</symbol>
</m:GetLastTradePrice>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
And here's the an example of a SOAP response:
HTTP/1.1 200 OK
Content-Type: text/xml;
charset="utf-8"
Content-Length: nnnn
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
<SOAP-ENV:Body>
<m:GetLastTradePriceResponse xmlns:m="Some-URI">
<Price>34.5</Price>
</m:GetLastTradePriceResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The content of the HTTP request/response is usually referred to
as the payload, and as you can see the payload is
valid XML. Because other XML documents can be nested
inside the payload, XML namespaces are used to disambiguate
the SOAP tags from any other tags that might be used.
To use SOAP all you need is a client capable of opening
a socket connection to a web server, and a web server
capable of running a script/extension in response
to POST (or the new M-POST extension) HTTP directives.
The hard part is writing the code to parse the XML
payloads. You can use your favorite XML processor, of
course, or else you can download some code from the
DevelopMentor
website.
The nice thing about SOAP is that because it uses
HTTP it can easily pass through most firewalls
(which can also easily parse and refuse SOAP
requests). And the XML encoding of the payload
avoids the problems that arise with binary
encodings, especially where security is concerned --
parsing an XML document is a safe operation, after
all.
It should be noted that SOAP is not a new object model,
just a wire-level way of encoding requests and
responses. How the server interprets a SOAP request
is entirely up to it. SOAP is not a particularly
efficient protocol either, so you'll want to restrict
its use to higher-level methods.
How does SOAP affect the wireless world? Right now
the client has to be able to generate and parse XML,
so that limits its use to devices with a fair
bit of programmability. I can't see cellphones
using SOAP yet, but handhelds certainly can. What
remains is for web and application server vendors
to offer more support for SOAP.
For more information on SOAP, a good place
to start is Microsoft's website using this
link: http://msdn.microsoft.com/xml/general/soaptemplate.asp.
Eric Giguère is the author of Palm Database Programming: The Complete Developer's Guide and an upcoming book on the Java 2 Micro Edition.
He works as a developer in Sybase's Mobile & Embedded Computing division. Visit his website at www.ericgiguere.com
or send him mail at ericgiguere@ericgiguere.com