SOAP XML response reults problems in iPhone - iphone

Now i have used SOAP web service in my application and i am not getting the proper xml response results from the server, but the same web service is working fine in the Android side and they are getting the proper SOAP xml response. But it's not working in the iPhone side. So i donno which side having the problem, in the server side or client side(iPhone side only). I have tried ASHTTPRequest also, i am getting the same results.
this is the sample code for calling the web service using NSURL connection,
NSString *soapMessage = [NSString stringWithFormat:
#"<?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"
#"<ViewMemberShipPackagesResponse xmlns=\"http://tempuri.org/\">\n"
#"</ViewMemberShipPackagesResponse>\n"
#"</soap:Body>\n"
#"</soap:Envelope>\n"];
NSString* urlString=[NSString stringWithFormat:#"http://aaaaa.com/aaaaaa.asmx"];
NSURL *url = [NSURL URLWithString:urlString];
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/ViewMemberShipPackagesResponse" 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 )
{
receiveData = [[NSMutableData data] retain];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(#"didReceiveResponse");
[receiveData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(#"didReceiveData");
[receiveData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *urlDataString = [[NSString alloc] initWithData:receiveData encoding:NSASCIIStringEncoding];
NSLog(#"the urlDataString is %#", urlDataString);
}
But i am not getting the proper soap xml response from the server. This sample output response from the server,
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><ViewMembershipPackagesResponse xmlns="http://tempuri.org/"><ViewMembershipPackagesResult xsi:type="xsd:string">
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><ViewMemberShipPackagesResponse xmlns="http://tempuri.org/"><ViewMemberShipPackagesResult>6</ViewMemberShipPackagesResult><Package><ID>5</ID><Name>1 Month Package</Name><DateSpan>1 Month</DateSpan><Price>$29.99</Price><Description>Test</Description></Package><Package><ID>6</ID><Name>1 Month Package with Multiple Locations</Name><DateSpan>1 Month</DateSpan><Price>$89.99</Price><Description>This pacakge to have</Description><
/Package><Package><ID>7</ID><Name>12 Month Test Package</Name><DateSpan>12 Months</DateSpan><Price>$360.00</Price><Description>12 Month Package</Description></Package><Package><ID>4</ID><Name>Andy Test Package</Name><DateSpan>12 Months</DateSpan><Price>$49.99</Price><Description>Test package for a year</Description></Package><Package><ID>2</ID><Name>Golden Package</Name><DateSpan>6 Months</DateSpan><Price>$80.00</Price><Description>There are many variations of passages of Lorem Ipsum available, but the majority have suffered</Description></Package><Package><ID>1</ID><Name>Premium Package</Name><DateSpan>12 Months</DateSpan><Price>$120.00</Price><Description>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</Description></Package></ViewMemberShipPackagesResponse></soap:Body></soap:Envelope></ViewMembershipPackagesResult></ViewMembershipPackagesResponse> </soap:Body></soap:Envelope>.
Please guide me.
THanks!

change this :
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString * urlDataString = [[NSString alloc] initWithBytes: [receiveData mutableBytes] length:[receiveData length] encoding:NSUTF8StringEncoding];
NSLog(#"the  urlDataString is %#", urlDataString);
}

Related

WCF service not working for iPhone

I have a WCF service:
http://parentportal.technologyorg.com/ParentPortal.svc
which I have to access and get the data. but unfortunately I am getting some HTML code.
my code is as follows
-(IBAction)doit:(id)sender{
NSString *soapMessage = [NSString stringWithFormat:#"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://parentportal.technologyorg.com/ParentPortal.svc\"><SOAP-ENV:Body><GetDictionary></GetDictionary></SOAP-ENV:Body></SOAP-ENV:Envelope>"];
NSURL *url = [NSURL URLWithString:#"http://parentportal.technologyorg.com/ParentPortal.svc"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"GetStudentTerms" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"%#",theRequest);
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
webData = [NSMutableData data] ;
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(#"___didReceiveResponse");
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(#"Status code %d", [httpResponse statusCode]);
[webData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(#"___didReceiveData");
[webData appendData:data];
NSLog(#"%#",webData);
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(#"___connectionDidFinishLoading");
NSLog(#"DONE. Received Bytes: %d", [webData length]);
NSString *xmlData = [[NSString alloc]
initWithBytes:[webData mutableBytes]
length:[webData length]
encoding:NSUTF8StringEncoding];
}
I am not sure whats going wrong were if I NSLog it I am the output as some HTML code.
Can any one help me on this.
Try SudzC SOAP-based web service definition WSDL files
Code generation for Objective-C libraries for iPhone development.
Follow the steps below

Call WSDL soap method in iPhone

i am working on an iPhone project in which i have a wsdl which is having description of methods and these methods contains an xml. I need to call these methods which are there in wsdl to get the xml using soap and parse it. I have read so many links and tutorials but was not able to to call the method in the wsdl. Please if u can provide me some links or suggestions.
you can use http://sudzc.com/ ,it gives you ready WSDL
This is a .NET web site solution that uses XSLT to transform SOAP-based web service definition WSDL files into complete code packages. The code that is generated is easy to read and update.
While SudzC supports multiple platforms, it's focus is on code generation for Objective-C libraries for iPhone development.
reference http://code.google.com/p/sudzc/
Please try this code. It worked for me.
[Note: GetDictionary is my method name which is displayed in Web service.and in url please write the url displayed in web service at below portion. It should be matched with url you have written in coding otherwise you will not get output(better copy it from web service)]
-(IBAction)loadData
{
NSString *soapMessage = [NSString stringWithFormat:
#"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<SOAP-ENV:Body>\n"
"<GetDictionary></GetDictionary>\n"
"</SOAP-ENV:Body>\n"
"</SOAP-ENV:Envelope>"];
NSURL *url = [NSURL URLWithString:#"http://c70.narolainfotech.local/WCFTest/RestServiceImpl.svc/basic"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"urn:RestServiceImpl/GetDictionary" forHTTPHeaderField:#"soapAction"];
// [theRequest addValue: #"urn:RestServiceImpl/getDataBY_ID" 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");
}
}
In connectionDidFinishLoading write following code:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"DONE. Received Bytes: %d", [webData length]);
NSString *strData = [[NSString alloc]initWithData:webData encoding:NSUTF8StringEncoding];
NSLog(#"result : %#",strData);
NSString *result=nil;
NSString *str;
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
theXML = [theXML stringByReplacingOccurrencesOfString:#"<" withString:#"<"];
theXML = [theXML stringByReplacingOccurrencesOfString:#">" withString:#">"];
theXML = [theXML stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
//NSLog(#"XML from Web-Service ======= \n\n %#",theXML);
NSArray *array=[theXML componentsSeparatedByString:#"<GetDictionaryResult>"];
for(int i=1;i<[array count];i++)
{
str=[array objectAtIndex:i];
NSRange ranfrom=[str rangeOfString:#"</GetDictionaryResult>"];
result =[str substringToIndex:ranfrom.location];
}
NSLog(#"result ::: %#",result);
}
Just Hope that it'll work for you. :)

when i want to consume my web service occur run time error

i upload a web service on my website's server www.example.com/webservice/SampleService.asmx and the method that i want to connect to it is GetAllUserInfo, i use below code for consume this web service but occur runtime error,can anybody help me to consume this web service
I test this web service and work properly on visual studio
NSString *soapMsg =[NSString stringWithFormat:#"<?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>"
"<GetAllUserInfo xmlns=\"http://www.example.com/\">"
"</GetAllUserInfo>"
"</soap:Body>"
"</soap:Envelope>"];
NSURL *url = [NSURL URLWithString:#"http://www.example.com/SampleService.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMsg length]];
[req addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[req addValue:#"http://www.example.com/GetAllUserInfo" forHTTPHeaderField:#"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[req setHTTPMethod:#"POST"];
[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
You can get the full error message with this:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(#"data: %.*s", [data length], [data bytes]);
}

SOAP and REST Services in iPhone

I am learning parsing in iPhone, When i was searching for the tutorials, i came to know very little bit about SOAP and REST services.
Now i wanted to learn SOAP and REST services.
Do any one have theoretical knowledge of SOAP and REST?
Is there any simple tutorial available for the beginners?
Thanks a lot in advance.
see these links, alongwith comparison of both
http://www.ajaxonomy.com/2008/xml/web-services-part-1-soap-vs-rest
http://greatgandhi.wordpress.com/2010/06/16/soap-vs-rest-%E2%80%93-the-best-webservice/
http://www.petefreitag.com/item/431.cfm
SOAP GUIDE
http://sudzc.com/
REST GUIDE
http://stackoverflow.com/questions/630306/iphone-rest-client
SOAP Web Service
-(IBAction) read_data
{
NSString *soapMessage = [NSString stringWithFormat:
#"<?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>%#</Celsius>n"
"</CelsiusToFahrenheit>n"
"</soap:Body>\n"
"</soap:Envelope>\n",name.text
];
NSLog(#"soap Message= %#",soapMessage);
NSURL *url = [NSURL URLWithString:#"your URL "];
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");
}
}
-(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); // Here is your received Value from url
[theXML release];
}
Here is the Example of RESTful WebService

How to post and get asmx web serivce access using iPhone app?

I am new to iPhone app development. How can I post data and get data by accessing the asmx web service using iPhone app?
I think asmx webservices are SOAP webservices you should read my blog entry here -
http://www.makebetterthings.com/iphone/call-soap-web-service-from-iphone/
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.