NSURLConnection POST也调用相同URL的GET

时间:2010-04-02 17:33:31

标签: objective-c nsurlconnection

我有一个NSURLConnection,这是服务器的帖子,但我希望它能返回一些小数据,无论它是否成功。

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

问题是,它不仅发送了一个帖子的URL,它还发送了一个GET,并且GET响应作为数据返回......我有点困惑。我查看了他的wireshark输出,它绝对是一个帖子和一个得到。

你们觉得怎么样?

1 个答案:

答案 0 :(得分:8)

网址是否通过重定向响应POST?您可以实现NSURLConnection委托方法connection:willSendRequest:redirectResponse:以查看是否属于这种情况(并取消不需要的重定向)。