1

I want to make a simple iOS application which calls a Web service.

I reffered the following link http://www.codeproject.com/Tips/622376/iOS-Soap-Webservice-Calling-and-Parsing-the-Respon

I tried to run the sample project that i got from this link.But I am getting the response like

"soap:ClientServer did not recognize the value of HTTP Header SOAPAction: http://tempuri.org/."

I am not able to find out what the reason behind this error is.And I am very much new to web services.Can anyone help me to solve this error?And someone please tell me ,what are the basic steps to call a web service from an iOS application.Is there any special version requirements of Xcode and iOS for calling web services?Please help me ...Thanks in advance for any help...

This is the code that I am using for sending the request

 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" ,textFieldCelcisus.text];


    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/" 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] ;
        NSLog(@"webdata is %@",webData);
        NSLog(@"Problem");
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }
Vips_iOS
  • 13
  • 1
  • 5

2 Answers2

2

Change this line:

[theRequest addValue: @"http://tempuri.org/" forHTTPHeaderField:@"SOAPAction"];

into:

[theRequest addValue: @"http://www.w3schools.com/webservices/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"];

The server is complaining about the value for the SOAPAction. The SOAPAction's are defined in the WSDL, and are different for each operation.

Since you provided the service URL and the service is public, i just checked it. The SOAPAction for 'CelsiusToFahrenheit' appeared to be: http://www.w3schools.com/webservices/CelsiusToFahrenheit

Note that each operation has it's own SOAPAction, e.g.:

CelsiusToFahrenheit -> http://www.w3schools.com/webservices/CelsiusToFahrenheit
FahrenheitToCelsius -> http://www.w3schools.com/webservices/FahrenheitToCelsius
etc...
Leijonien
  • 1,415
  • 14
  • 16
  • Thank you very much Leijonien..It worked. Can you please help me to understand the topic in detail.Is there any problems in using SOAP web services?Which one is the best in SOAP and REST. – Vips_iOS Nov 16 '13 at 19:13
  • There's no problem at all using SOAP or REST interfaces in your apps, I'm using them all the time ;-). And about SOAP vs REST... It appears seems you're building a client, so it's not up to you what to use. It's the server who determines the interface and protocols. For some general info on the subject check out the following threads: http://stackoverflow.com/questions/209905/representational-state-transfer-rest-and-simple-object-access-protocol-soap and http://stackoverflow.com/questions/209905/representational-state-transfer-rest-and-simple-object-access-protocol-soap – Leijonien Nov 16 '13 at 19:25
  • Thank you very much friend..That link clearly explains what the difference between SOAP and REST is.I tried an application with REST also.It seemed simpler.But it failed because of bad url.:( – Vips_iOS Nov 16 '13 at 19:31
  • Yes, REST is a much simpler protocol, but because of that it needs more validation on the client side. When using SOAP you (the client) knows exactly what you can expect as response. If you run into specific issues or have questions, don't hesitate to ask. – Leijonien Nov 16 '13 at 19:35
  • I just started learning this today.So I am not aware of web services at all.Can u Please suggest some links or provide some information about this.So many links are there but I am having confusion in many things..Thank you very much for your concern. God bless :) – Vips_iOS Nov 16 '13 at 19:46
  • That question is too broad for me ;-) Adding more links to that list would probably confuse you even more, because i don't know your background and what you are exactly looking for. My suggestion would be to just continue from where you are now: your working example, try to build what you need and when you run into any specific issues or questions i'll be glad to help you out if i can. – Leijonien Nov 16 '13 at 20:08
1

Above answer is right, only suggestion that use NSURLSession instead of NSURLConnection.

Tushar Thakar
  • 322
  • 2
  • 8