I am trying to use this web service in my app (just to try my hands on web services): http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit and below is the code i am using:
- (void)viewDidLoad {
[super viewDidLoad];
// create a soap Message which is given in your required web service
// create a url to your asp.net web service.
NSURL *tmpURl=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit"]];
// create a request to your asp.net web service.
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:tmpURl];
NSString * params = [[NSString alloc] initWithFormat:@"Celsius = 32"];
myWebData = [[NSMutableData alloc] init];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
}
// a method when connection receives response from asp.net web server
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[myWebData setLength: 0];
}
// when web-service sends data to iPhone
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[myWebData appendData:data];
NSString *receivedDataString = [[NSString alloc] initWithData:myWebData encoding:NSUTF8StringEncoding];
NSLog(@"%@",receivedDataString);
}
// when there is some error with web service
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
// when connection successfully finishes
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// check out your web-service retrieved data on log screen
NSString *theXML = [[NSString alloc] initWithBytes: [myWebData mutableBytes] length:[myWebData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",theXML);
[theXML release];
}
In the NSString theXML i am getting some response which had some erro too. Below is the response which i am getting in debugger:
WebServiceTutorial[474:207] soap:ReceiverServer was unable to process request. ---> Data at the root level is invalid. Line 1, position 1.
Thanks,