1

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,

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Ashutosh
  • 5,614
  • 13
  • 52
  • 84
  • 1
    When ever you use w3schools make sure you check this site too: http://w3fools.com/ – vikingosegundo Jun 19 '11 at 23:19
  • Not sure, if this is a part of the problem, but `NSString * params = [[NSString alloc] initWithFormat:@"Celsius = 32"];` might give bad behavior, as you are not passing in a format. try `params = @"Celsius = 32"` instead. BTW: You are leaking `params` and `myWebData` – vikingosegundo Jun 19 '11 at 23:25

2 Answers2

0

You are sending the request to the wrong URL. Try NSURL *tmpURl=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit"]];

Apart from that you should probably set Content-Type and Content-Length:

NSString *postLength = [NSString stringWithFormat:@"%d", [params length]];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
Sascha
  • 5,903
  • 3
  • 24
  • 20
0

I believe what you're getting is a .NET error. The reason is probably because you are connecting to a SOAP web service. In iOS, there is no native SOAP protocol implementation.

Essentially SOAP works by sending some standard XML into the body of the request sent to the server. Just connecting to that web address does not mean you are communicating with the web service, which is why .NET is giving you the error. Even though you go to that web address with your browser and it appears to work, the site itself is doing all of the SOAP-ing for you so you aren't aware of it.

You have two options. Either create a soap request from scratch or use a REST service in your test. REST services act just like you are assuming this one works. REST services are just a fancy way of saying "web services that can be accessed simply by going to the http address."

Here's another SOAP example

Community
  • 1
  • 1
Vinnie
  • 1,670
  • 14
  • 16