How to make a WSDL web service in iOS?

This post will describe about the basic web service on iOS applications.

Lets assume the following .wsdl url is the source URL.

http://www.webservicex.net/geoipservice.asmx?WSDL 

Create a SOAP Envelop and SOAP Header.

NSMutableString * soapHeader = @”<soapenv:Envelope xmlns:soapenv=\”http://schemas.xmlsoap.org/soap/envelope/\””

            ” xmlns:hom=\”http://www.webservicex.net/“>”

            “<soapenv:Header/>”;

Be remember that “xmlns:hom”, text next to “:” is the word which will be used as the targetnamespace reference for the service call which will be added in SOAP body.

SOAP body will be as follows :

[soapHeader appendString:@”<soapenv:Body>”];

[soapHeader appendString:@”<hom:GetGeoIP>”]; // add the body parameters

[soapHeader appendString:@”<hom:IPAddress>0.0.0.1</hom:IPAddress>”];

[soapHeader appendString:@”</hom:GetGeoIP>”];

[soapHeader appendString:@”</soapenv:Body>”];

[soapHeader appendString:@”</soapenv:envelope>”];

Now the web service is as follows:

NSMutableString * requestStr = [NSMutableStringstring];

[requestStr appendString:soapHeader];

NSString * urlStr = @”http://www.webservicex.net/geoipservice.asmx?WSDL&#8221;;

    NSURL * url = [NSURL URLWithString:urlStr];

    NSMutableURLRequest * request = [[NSMutableURLRequestalloc] initWithURL:url];

    [request setValue:@”http://www.webservicex.net/GetGeoIP” forHTTPHeaderField:@”SOAPAction”];

    [request setValue:@”http://www.webservicex.net/” forHTTPHeaderField:@”targetNamespace”];

    [request setValue:@”text/xml;charset=UTF-8″forHTTPHeaderField:@”Content-Type”];

    [request setHTTPBody:requestBody];

    [request setHTTPMethod:@”POST”];

    [request setValue:[NSStringstringWithFormat:@”%d”,[requestBody length]] forHTTPHeaderField:@”Content-Length”];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        BOOL isSuccess = YES;

        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

        NSLog(@”received data—%@”,response);

        if(connectionError != nil) {

            isSuccess = NO;

            NSLog(@”connection error with code %d”,connectionError.code);

        }

        NSDictionary * responseDictionary = [NSDictionary dictionary];

        if([httpResponse statusCode] == 200) {

            isSuccess = YES;

         //Do something with the received dictionary.

    }];

This is the basic platform how we can make a simple SOAP based web service calls in iOS.

Happy coding !!!!

Advertisements