如何从我从Web服务获得的响应中获取确切的值?

时间:2011-06-19 20:24:03

标签: iphone objective-c cocoa-touch web-services

我正在尝试在我的应用中使用此网络服务(只是尝试使用网络服务):http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit以下是我正在使用的代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    // create a soap Message which is given in your required web service

    // create a url to your asp.net web service.
    NSURL *tmpURl=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit"]];

    // create a request to your asp.net web service.
    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:tmpURl];
    NSString * params = [[NSString alloc] initWithFormat:@"Celsius = 32"];
    myWebData = [[NSMutableData alloc] init];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
    connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
}
    // a method when connection receives response from asp.net web server
    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        [myWebData setLength: 0];
    }
    // when web-service sends data to iPhone
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        [myWebData appendData:data];
        NSString *receivedDataString = [[NSString alloc] initWithData:myWebData encoding:NSUTF8StringEncoding];
        NSLog(@"%@",receivedDataString);


    }
    // when there is some error with web service
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
    }
    // when connection successfully finishes 
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        // check out your web-service retrieved data on log screen
        NSString *theXML = [[NSString alloc] initWithBytes: [myWebData mutableBytes] length:[myWebData length] encoding:NSUTF8StringEncoding];
        NSLog(@"%@",theXML);
        [theXML release];

    }

在NSString theXML中,我得到了一些响应,也有一些错误。 以下是我在调试器中得到的响应:

  

WebServiceTutorial [474:207] soap:ReceiverServer无法   处理请求。 --->数据在   根级别无效。第1行,   位置   1。

谢谢,

2 个答案:

答案 0 :(得分:0)

您正在将请求发送到错误的网址。 试试NSURL *tmpURl=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit"]];

除此之外,你应该设置Content-Type和Content-Length:

NSString *postLength = [NSString stringWithFormat:@"%d", [params length]];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];

答案 1 :(得分:0)

我相信你得到的是一个.NET错误。原因可能是因为您正在连接到SOAP Web服务。在iOS中,没有本机SOAP协议实现。

本质上,SOAP通过将一些标准XML发送到发送到服务器的请求主体来工作。只是连接到该Web地址并不意味着您正在与Web服务进行通信,这就是.NET为您提供错误的原因。即使您使用浏览器访问该网址并且它似乎正常工作,该网站本身也会为您执行所有SOAP-ing,因此您不了解它。

您有两种选择。 create a soap request from scratch或在测试中使用REST服务。 REST服务就像你假设这个工作一样。 REST服务只是一种说法“只需通过访问http地址即可访问的Web服务”的一种奇特方式。

Here's another SOAP example

相关问题