开发者

didReceiveData of NSMutableUrlRequest never triggered

I’m new in iphone development world and I’m trying to call a web service using the HTTP method POST.

To do that I'm using the NSMutableUrlRequest. My problem is that the DidReceiveData delegate is never called and the NSUrlConnection doesn’t return null.

Here is my code :

- (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];
    [reque开发者_如何学Cst 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");
}

Someone can tell me if I forgot something important please ? Thanks a lot.


it looks like you are using a SOAP webservice. Take a look at this tutorial, which goes through everything you need to know about calling a webservice on the iPhone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜