Iphone – NSMutableURLRequest setTimeoutInterval issue

iphonensmutableurlrequest

Ok firstly if I use a NSURLReuqest(non mutable) as following, the the connection do timeout accordingly to what was set.
The weird thing is why does the NSLog always read 0?

self.requestURL = [NSURLRequest requestWithURL:[NSURL URLWithString:requestString]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
NSLog(@"request timeOutInterval:%d", self.requestURL.timeoutInterval); // always 0

Next, I do something like this and the timeoutInterval does not get set.

self.requestURL = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:requestString]] autorelease];
[self.requestURL setTimeoutInterval:20];
NSLog(@"request timeOutInterval:%i", self.requestURL.timeoutInterval); // same thing always 0 here.

EDIT. I am using %f to log the timeoutInterval property now and both reads 20.000. But the real problem is why was my my NSMutableURLRequest not firing the - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error delegate call back method when it reaches the timeoutInterval(20s). Instead it is only timed out at around the 75s. Even longer than the default of 60s…

Even if I remove the [self.requestURL setTimeoutInterval:20]; line, the connection still timeout at 75s.

I have tried

self.requestURL = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:requestString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0] autorelease];

Best Solution

Try this

NSLog(@"request timeOutInterval:%f", self.requestURL.timeoutInterval); It worked for me.

UPDATE

Check this answer by @Francois in this previous SO question

There's a thread on Apple dev forums discussing this issue. Apparently on iPhone OS, the setter mandates timeoutInterval a minimum of 240 seconds (4 minutes). This only occurs when the postBody is not empty (typically when using a POST request). This seems crazy, but apparently it's there to make sure requests leave the system even though it might take many seconds for the WWAN (3G) interface to wake up. 240 seconds seems rather steep, so they suggest to set a timer and cancel the asynchronous connection when your timer fires. I know this seems stupid, but that's the only I managed to get timeout for POST requests...

Related Question