Simple Object Access Protocol (SOAP) is a standard protocol specification for message exchange based on XML. Communication between the web service and client happens using XML messages. For one of the mobile clients I was working on, I had to write a client for communicating with a SOAP service. Sending a SOAP request is quite easy using the [cci lang="javascript"]Titanium.Network.HTTPClient[/cci] object.
[cc lang=“javascript” lines=“-1”] /**
- Get the beginning of request envelope for SOAP request */ function getRequestEnvelopeBegin() { return ’<env:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:env=”http://schemas.xmlsoap.org/soap/envelope/“><env:Header></env:Header><env:Body>’; }
/**
- Get the end of request envelope for SOAP request */ function getRequestEnvelopeEnd() { return ’</env:Body></env:Envelope>’; }
/* **
- Get the a new XHR object */ function getXHR() { var xhr; //Grab an XMLHTTPRequest Object xhr = Titanium.Network.createHTTPClient(); return xhr; }
/**
- Sends a SOAP request and calls the callback
- @param {Function} callback */ function sendSOAPRequest(callback) { var xhr = getXHR(); // Set callback function xhr.onload = function() { Ti.API.info(‘Received response for soap request.’); callback.call(this, this.responseXML); // call the callback with response xml };
// Create body for request var soapRequest = getRequestEnvelopeBegin() + ’’ + getRequestEnvelopeEnd();
// Send soap request xhr.open(‘POST’, url); xhr.setRequestHeader(‘Content-Type’, ‘text/xml’); xhr.setRequestHeader(‘SOAPAction’, SOAPREQUESTACTION); xhr.send(soapRequest); } [/cc]
One important piece of information in the SOAP request other than the body is the header for the request. Most of the servers wouldn't handle your requests until the headers for the requests are what they expect.