To call a webservice from an iOS application

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

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, eg:

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

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

链接地址: http://www.djcxy.com/p/71392.html

上一篇: 宁静与肥皂

下一篇: 从iOS应用程序调用Web服务