views:

1798

answers:

3

I have the following problem. On a NSMutableURLRequest using the HTTP method POST the timeout interval set for the connection is ignored. If the internet connection has a problem (wrong proxy, bad dns) the url request fails after about 2-4 minutes but not with NSLocalizedDescription = "timed out"; NSUnderlyingError = Error Domain=kCFErrorDomainCFNetwork Code=-1001 UserInfo=0x139580 "The request timed out.

If the http method used is GET it works fine. The connection is async over https.

    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 

    [request setTimeoutInterval:10];

    //Set the request method to post
    [request setHTTPMethod:@"POST"];

    if (body != nil) {
     [request setHTTPBody:body];
    }

    // Add general parameters to the request
    if (authorization){
     [request addValue: authorization forHTTPHeaderField:@"Authorization"];
    }
    [request addValue: WS_HOST forHTTPHeaderField:@"Host"];
    [request addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];

    [[NSURLCache sharedURLCache] setDiskCapacity:0];

    [self addToQueueRequest:request withDelegate:delegate];

'
A: 

I have the same problem. Timeout interval works for a GET method but not a POST method. Anyone know how to fix this?

alec
+1  A: 

According to a post on the Apple developer forum, the minimum timeout interval for POST is 240 seconds. Any timeout interval shorter than that is ignored.

If you require a shorter timeout interval, use an async request along with a timer and call cancel on the NSURLConnection as needed.

link to thread: here

Clay Chambers
+1  A: 

Fixed with Clay Chambers's suggestion: with a custom timer Added a timer in a subclass of NSUrlConnection

if (needsSeparateTimeout){

    SEL sel = @selector(customCancel);

    NSMethodSignature* sig = [self methodSignatureForSelector:sel];

    NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:sig];

    [invocation setTarget:self];

    [invocation setSelector:sel];

    NSTimer *timer = [NSTimer timerWithTimeInterval:WS_REQUEST_TIMEOUT_INTERVAL invocation:invocation repeats:NO];

    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

}

}

In the custom cancel method the connection is cancelled

[super cancel];