Using Webservices ( SOAP ) in BREW

This post deals with making the SOAP based Webservices API to work in the BREW environment. It is not intended to be a tutorial on SOAP or webservices.
There has been a lot of confusion regarding whether the webservices API are supported in BREW or not. The correct answer is No, because BREW does not have a XML parser or a SOAP abstraction layer on its own, which is a critcal component for SOAP based webservices. However, if you are ready roll up your sleeves and get your hands dirty with low level stuff you can make SOAP based webservices work in BREW environment.
The SOAP based webservices at it core are nothing but the exchange of XML messages in accordance with the WSDL document of the webservice being used. The sample webservice we will be using now is GetAtomicNumber Operation of Periodictable API provided by Webservicex.net. The XML Parser used is a basic compact ANSI C XML parser written by Martyn Brown and modified to work in BREW environment.
As mentioned above, to use the Periodic table webservice API, all we need to do is to POST a valid HTTP request using IWEB API with appropriate headers and the XML request as the body. The response we get will contain the result in XML format.

Request and Response XML
The structure of the XML messages to retrive the AtomicNumber of a given element is as below. In the SOAP Request the query has to be a valid element name. In the SOAP Response the value contains the result.

SOAP Request

Header Information
POST /periodictable.asmx
HTTP/1.1
Host: http://www.webservicex.net/
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.webserviceX.NET/GetAtomicNumber"
XML Request
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetAtomicNumber xmlns="http://www.webservicex.net/">
<ElementName>query</ElementName>
</GetAtomicNumber>
</soap:Body>
</soap:Envelope>

SOAP Response

Header Information
HTTP/1.1 200 OK
Content-Type:
text/xml; charset=utf-8
Content-Length: length
XML Response
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetAtomicNumberResponse xmlns=""http://www.webservicex.net/">
<GetAtomicNumberResult>value</GetAtomicNumberResult>
</GetAtomicNumberResponse>
</soap:Body>
</soap:Envelope>

Making a SOAP Web Request in BREW.You have to create the XML request and hold it in the buffer.
char request[1024] = "<soap:Envelope
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> <soap:Body> <GetAtomicNumber xmlns=\"http://www.webservicex.net/\">
<ElementName>%s</ElementName>
</GetAtomicNumber> </soap:Body>
</soap:Envelope>";
The %s in the element name tag has to be replaced by the name of the element for which the AtomicNumber is requested. The variable pszElement contains the name of the element. Now, prepare the ISource pointer for the request.
ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_SOURCEUTIL, (void **)&(pMe->sourceUtil));
nLen = SNPRINTF(NULL, 0, request, pszElement);
pMe->postData = (char*)MALLOC(sizeof(char)*(nLen+1));
SNPRINTF(pMe->postData, nLen, request, pszElement);
ISOURCEUTIL_SourceFromMemory(pMe->sourceUtil, pMe->postData, nLen, NULL, NULL, &(pMe->source));
Now, initialize the callback and make the HTTP request using IWEB API.
CALLBACK_Init(&pMe->callback, webservices_GotResponse, pMe);
IWEB_GetResponse(pMe->web, (pMe->web, &pMe->webresp, &pMe->callback, "http://www.webservicex.net/periodictable.asmx",
WEBOPT_HANDLERDATA, pMe,
WEBOPT_METHOD,"POST",
WEBOPT_HEADER, "Content-Type:text/xml\r\n SOAPAction:http://www.webserviceX.NET/GetAtomicNumber\r\n",
WEBOPT_CONTENTLENGTH, pMe->postLen-1,
WEBOPT_BODY, pMe->source,
WEBOPT_HEADERHANDLER, webservices_PrintHeader,
WEBOPT_STATUSHANDLER, webservices_PrintStatus,
WEBOPT_END));

Once you have got the successfull response, you need to retrive the XML response sent by the server and parse it to get the result.
McbXMLResults pResults;
McbXMLElement *pRoot = NULL;
McbXMLElement *pResult = NULL;
pRoot = McbParseXML(pMe->response, &pResults);
pResult = McbFindElement(pRoot,
"xml/soap:Envelope/soap:Body/GetAtomicNumberResponse/GetAtomicNumberResult");
Now, the variable pResult->pEntries->node.pText->lpszValue will have the result. The complete sample application can be downloaded here. The application was developed using BREW SDK 3.1.5.

3 Responses to "Using Webservices ( SOAP ) in BREW"

Unknown responded on November 25, 2009 at 9:21 AM #

Hi, First thanks for taking the time to write tutorials about BREW. I would like to try your approach for using web services in brew but the link to the source code it's broken; Would you please give me another link to download it or perhaps send it to my email?. I tried to code by myself your solution but I am having some problems with the xml soap message I'm sending to the server, Thanks, Jose.

Kamalakshan responded on March 15, 2010 at 5:43 AM #

The link for the zip file download is broken can you fix it.


Thank you

Aniruddha responded on June 8, 2013 at 3:03 AM #

Can anyone send me source brew mp source code to parse XML ?
Email : aniruddha.pcskull@gmail.com

Thanks in advance !!