NSMutableUrlRequest的didReceiveData从未触发过

时间:2009-10-10 15:22:38

标签: iphone objective-c http post delegates

我是iphone开发世界的新手,我正在尝试使用HTTP方法POST调用Web服务。

要做到这一点,我正在使用NSMutableUrlRequest。我的问题是从未调用过DidReceiveData委托,NSUrlConnection不返回null。

这是我的代码:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
{
    NSLog(@"didReceiveData");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"didFinishLoading");
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError");
}

- (void) HttpRequest
{
    NSLog(@"Calling HttpRequest");

    NSString *parameters = @"placeName=Restau";
    NSData *postData = [parameters dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    NSURL *webServiceUrl = [NSURL URLWithString:@"http://api.malves.fr/API.asmx/EngineSearchPlaceService"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:webServiceUrl];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"keep-live" forHTTPHeaderField:@"Connection"];
    [request setValue:@"300"  forHTTPHeaderField:@"Keep-Alive"];
    [request setHTTPBody:postData];

    NSURLConnection *connectionResponse = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (!connectionResponse)
    {
        NSLog(@"Failed to submit request");
    }
    else
    {
        NSLog(@"Request submitted");
    }

    NSLog(@"Sleeping... ZZZZzzzzzz");
    [NSThread sleepForTimeInterval:10];
    NSLog(@"Sleep done");
}

有人可以告诉我,如果我忘了重要的事吗?非常感谢。

1 个答案:

答案 0 :(得分:0)

看起来您正在使用SOAP Web服务。看看这个tutorial,它将介绍在iPhone上调用网络服务时需要了解的所有信息。