ios - 从 iOS 应用程序调用网络服务

标签 ios xml web-services soap

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

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

我尝试运行从该链接获得的示例项目。但我得到的响应如下

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

我无法找出这个错误背后的原因是什么。而且我对网络服务非常陌生。有人能帮我解决这个错误吗?有人请告诉我,调用的基本步骤是什么来自 iOS 应用程序的 Web 服务。调用 Web 服务时 Xcode 和 iOS 是否有任何特殊版本要求?请帮助我...提前感谢您的帮助...

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

 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

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

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

关于ios - 从 iOS 应用程序调用网络服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20021392/

相关文章:

ruby - 在 Rails 3 中使用 Nokogiri 读取 XML 文件

java - UTF-16LE 编码和 xerces2 Java

java - SOAP org.xmlpull.v1.XmlPullParserException : expected: START_TAG

android - 通过 Android 保护对 Web 服务的请求

ios - Xcode 在本地通知后显示特定 View

ios - 几乎赤裸裸的iOS8.4 --> 如何获取链接器?

java - TPE 1105 用于 BulkRequestTransmissionStatus A2A IRS

ios - iPhone 6s - iOS 9.1 在 [UICollectionViewController previewingContext :viewControllerForLocation:] 上崩溃

ios - Swift 处理日期更改事件

asp.net - asmx 网络服务不读取 postdata