NSMutableURLRequest timeout interval not taken into consideration for POST requests

user177844 picture user177844 · Sep 23, 2009 · Viewed 39.9k times · Source

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];

'

Answer

user214852 picture user214852 · Nov 19, 2009

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