views:

315

answers:

1

Hi all, I have a NSURLConnection which is a post to the server, but I expect it to return some small data, whether it was successful or not.

        -(void)submitPost:(NSString *)xml
    {
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[service generateURL]];
        NSString *result = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)xml, NULL, CFSTR("?=&+"), kCFStringEncodingUTF8);
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:[result dataUsingEncoding:NSUTF8StringEncoding]];
        [request setHTTPMethod:@"POST"];
        NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
        if(theConnection)
        {
            NSLog(@"Connection success");
            [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
            [theConnection retain];
            failed = NO;
        }
        else 
        {
            NSLog(@"Connection failed");
        }
}

The problem is, not only does it send a post the URL, it also sends a GET, and the GET response is returned as the data... I'm a bit confused. I checked my wireshark output, and it's definitely making both a post and a get.

What do you guys think?

+4  A: 

Does the URL respond to a POST with redirect? You can implement the NSURLConnection delegate method connection:willSendRequest:redirectResponse: to see if that's the case (and to cancel an unwanted redirect).

dbarker
I just implemented that method, and it is being called. But I'm not sure what that means, could you explain it? I don't really understand the documentation either.
kodai
If that method is called, then it means that the response is a redirect - you can inspect the NSURLRequest parameter that's passed to this method to see where it's redirecting to. The request for the redirect URL is the extra GET you're seeing. If the redirect is causing a problem in your app, you can cancel or modify the redirect in this delegate method.
dbarker