从iOS应用程序调用Web服务

我想制作一个调用Web服务的简单的iOS应用程序。

我有以下链接http://www.codeproject.com/Tips/622376/iOS-Soap-Webservice-Calling-and-Parsing-the-Respon

我试图运行我从这个链接得到的示例项目。但是我得到的答复像

“soap:ClientServer无法识别HTTP Header SOAPAction的值: http : //tempuri.org/” 。

我无法找出这个错误背后的原因是什么。而且我对web服务非常陌生。任何人都可以帮助我解决这个错误?有人请告诉我,调用Web服务的基本步骤是什么?从iOS应用程序。是否有Xcode和iOS调用Web服务的任何特殊的版本要求?请帮助我…预先感谢任何帮助…

这是我用来发送请求的代码

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"); } 

改变这一行:

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

成:

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

服务器正在抱怨SOAPAction的值。 SOAPAction的定义在WSDL中 ,并且对于每个操作都是不同的。

由于您提供的服务url和服务是公开的,我只是检查它。 “CelsiusToFahrenheit”的SOAPAction似乎是: http://www.w3schools.com/webservices/CelsiusToFahrenheit : http://www.w3schools.com/webservices/CelsiusToFahrenheit

请注意,每个操作都有自己的SOAPAction,例如:

 CelsiusToFahrenheit -> http://www.w3schools.com/webservices/CelsiusToFahrenheit FahrenheitToCelsius -> http://www.w3schools.com/webservices/FahrenheitToCelsius etc... 

以上答案是正确的,只是build议使用NSURLSession而不是NSURLConnection。