SOAP in iOS Application

I’m trying to develop an app that uses data from a SOAP service. is the first time I use a SOAP service. After reading several tutorials I thought I would be able to set the request to the service in the right way but I always get errors in the response. What’s wrong ?!

NSString *soapMessage = [NSString stringWithFormat:@"<?xml version="1.0" encoding="UTF-8"?>n"
                 "<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="urn:Lottery.Intf-ILottery" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:enc="http://www.w3.org/2003/05/soap-encoding">n"
                 "<env:Body>n"
                 "<ns1:GetUpcomingLotteries env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">n"
                 "<EntityID xsi:type="xsd:int">2</EntityID>n"
                 "<Password xsi:type="xsd:string">Smart@Winners</Password>n"
                 "<SortBy xsi:type="xsd:int">0</SortBy>n"
                 "<limit xsi:type="xsd:int">0</limit>n"
                 "</ns1:GetUpcomingLotteries>n"
                 "</env:Body>n"
                 "</env:Envelope>"];


NSURL *sRequestURL = [NSURL URLWithString:@"xxxxxxxxx"];
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:sRequestURL];
NSString *sMessageLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

[myRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[myRequest addValue: @"urn:Lottery.Intf-ILottery/GetUpcomingLotteries" forHTTPHeaderField:@"SOAPAction"];
[myRequest addValue: sMessageLength forHTTPHeaderField:@"Content-Length"];

[myRequest setHTTPMethod:@"POST"];
[myRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:myRequest returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];


Source: xml

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.