views:

220

answers:

1

Hi

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

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

A: 

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.

zPesk
My webservices support both soap and simple post requests
Michael Alves