Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLConnection POST also calls GET of same URL

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?

like image 716
kodai Avatar asked Feb 27 '23 06:02

kodai


1 Answers

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).

like image 107
dstnbrkr Avatar answered Mar 07 '23 14:03

dstnbrkr