Dealing with SOAP services and decoding WSDL files tagged: ,

Dealing with SOAP services and decoding WSDL files

Posted by in iOS, Web Services

A lot of systems use SOAP services. For some basic info on SOAP services, check out this link. It’s not ideal for interacting with mobile applications, but if you are building an app that needs to interact with SOAP services here’s how.

If no documentation is provided, you typically have to decode a WSDL file. A handy tool you can use to decode this file is Oxygen XML editor. To use this tool, go to Tools->WSDL SOAP Analyser.

Enter the URL of the WSDL file and select Open.

A right panel will appear showing you the operations you can call through this SOAP service. It displays the URL you to request, the SOAP action to set and a sample XML request and response. All you need to do in your app is to construct the XML according to the request example. Make a HTTP POST the request and set the SOAP action as a POST variable.

Here’s a simple request built for the iPhone and using the ASIHttpRequest library.

NSString *soapMessage = @"<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">\n"
"<SOAP-ENV:Header/>\n"
"<SOAP-ENV:Body>\n"
"<oxy:ItemQuery xmlns:oxy="urn:BisqwitAjaxSoapDemo" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">\n"
"<item>hello world</item>\n"
"</oxy:ItemQuery>\n"
"</SOAP-ENV:Body>\n"
"</SOAP-ENV:Envelope>";

NSURL *url = [NSURL URLWithString:@"http://bisqwit.iki.fi/jutut/kuvat/ajaxsoapdemo/api"];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
[request setTimeOutSeconds:60];
[request addRequestHeader: @"Content-Type" value:@"text/xml; charset=utf-8"];
[request addRequestHeader: @"SOAPAction" value:@"urn:BisqwitAjaxSoapDemoAction"];
[request addRequestHeader: @"Content-Length" value:msgLength];
[request setRequestMethod: @"POST"];
NSMutableData *data = (NSMutableData*)[soapMessage dataUsingEncoding:NSUTF8StringEncoding];
[request setPostBody:data];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestWentWrong:)];
[request startAsynchrounous];
[request release];

When you receive the response, you can decode this using the NSXMLParser. This is a SAX parser.

- (void)requestDone:(ASIHTTPRequest *)request {
NSData *data = [request responseData];
NSString *rawXML = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"rawXML);
xmlParser = [[NSXMLParser alloc] initWithData:data];
[xmlParser setDelegate:self];
[xmlParser setShouldProcessNamespaces:NO];
[xmlParser setShouldReportNamespacePrefixes:NO];
[xmlParser setShouldResolveExternalEntities:NO];
[xmlParser parse];
}