May 28, 2010
Call SOAP web service from iPhone
Here is my experience with calling SOAP web services from iPhone SDK.
For this tutorial i am using w3school’s CelsiusToFahrenheit SOAP web service example here you can find the docs for this service.
To call a SOAP service first i create a string with the SOAP request as follows.
NSString *soapMessage = @"<?xml version="1.0" encoding="utf-8"?>n"
"<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/">n"
"<soap:Body>n"
"<CelsiusToFahrenheit xmlns="http://tempuri.org/">n"
"<Celsius>50</Celsius>n"
"</CelsiusToFahrenheit>n"
"</soap:Body>n"
"</soap:Envelope>n";
After creating the SOAP request I create a NSMutableRequest to send this request to server.
NSURL *url = [NSURL URLWithString:@"http://w3schools.com/webservices/tempconvert.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
After firing the request we can collect the XML response in the NSURLConnection’s delegate methods.
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConenction");
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",theXML);
[theXML release];
}
After collecting the XML response in theXML string in -(void)connectionDidFinishLoading:(NSURLConnection *)connection
we can parse this string using TBXML for any other XML parser you like.
Hi, did you use any API for this? iphone sdk natively supports soap? tnx
[...] use this Link for using SOAP Webservice in [...]
Hi
How can you parse two XMLs using the method you have provided?
The above method is not for parsing xmls. That is only to get soap response from server. You need some xml parser to parse xmls. You can try TBXML (http://www.tbxml.co.uk/TBXML/TBXML_Free.html). TBXML is quite easy.
[...] http://www.makebetterthings.com/blogs/iphone/call-soap-web-service-from-iphone/ [...]
hi,i use that code but u didnt how to use to run web service app exactly..i mean if u show use step by step it would be better or any idea u have pls help i need ….
Actually I am in middle of writing something on web services. I’ll post soon on MBT.. meanwhile keep checking other post and subscribe to RSS.. Thanks..
thnk u we waiting for u..
I built soap service and tested by calling soap service from jquery.ajax. It worked well.
I follow the tut, but get the response is Bad Request
could you give me the hint!
thanks.
HI HOW TO GIVE USER CREDEDENTIAL LIKE USER NAME AND PASSWORD BY SENDING SOAP REQUEST TO ACCESS SOAP RESPONSE THROUGH UI TEXTBOX
Dear Saurabh ,
I am iPhone apps developer , working on soap but when iperform nslog of theXMl(in your) it show error and error is “Request format is invalid: Missing required soap:Body”
and my soap message is given below:@”\n”
“/\n”
“/\n”
“<PDALogin xmlns=\"http://tempuri.org/\n"
"%@\n”
“%@\n”
“\n”
“\n”
“\n”
I used this site for parsing
http://mws.nameserverkatie.com/ws/mws.asmx?op=PDALogin
where i am wrong please help me
Good one,
Can be very good if you would able to provide parser and related all information…
but any way nice
want to change one thing in “soapMessage” as below
NSString *soapMessage = @”\n”
“\n”
“\n”
“\n”
“50\n”
“\n”
“\n”
“\n”;
Ok,
whole string not post here…!?!
Sorry
manage yourself
how to create soap request?
please send me full code of xcode, web service with example…