从 iOS 应用程序调用 Web 服务 [英] To call a webservice from an iOS application

查看:36
本文介绍了从 iOS 应用程序调用 Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我参考了以下链接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 无法识别 HTTP 标头 SOAPAction 的值:http://tempuri.org/."

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

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

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

推荐答案

更改这一行:

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

进入:

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

服务器抱怨 SOAPAction 的值.SOAPAction 在 WSDL 中定义,并且对于每次操作.

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

由于您提供了服务 URL 并且该服务是公开的,所以我刚刚检查了它.'CelsiusToFahrenheit' 的 SOAPAction 似乎是:http://www.w3schools.com/webservices/CelsiusToFahrenheit

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

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

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...

这篇关于从 iOS 应用程序调用 Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆