2011-06-19 96 views
1

我想使用此Web服务在我的应用程序(只是为了尝试Web服务我的手):http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit以下是我使用的代码:如何从我从Web服务获得的响应中获得准确的值?

- (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]皂:ReceiverServer无法 处理请求。 --- >数据在 根级别无效。 1号线, 位置 1.

感谢,

+1

当曾经使用W3Schools的请务必检查这个网站太:http://w3fools.com/ – vikingosegundo

+0

不知道,如果这是问题的一部分,但'的NSString * PARAMS = [[ NSString alloc] initWithFormat:@“Celsius = 32”];'可能会给出不好的行为,因为你没有传入格式。尝试'params = @“Celsius = 32”'代替。顺便说一句:你正在泄漏'params'和'myWebData' – vikingosegundo

回答

0

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

除此之外,你应该设置Content-Type和内容长度:

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

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

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

您有两种选择。 create a soap request from scratch或在您的测试中使用REST服务。 REST服务的行为就像你认为这个服务起作用。 REST服务只是表达“可通过访问http地址访问的Web服务”的一种奇特方式。

Here's another SOAP example