好的,首先,如果我如下使用 NSURLReuqest(non mutable),则连接会根据设置超时。奇怪的是为什么 NSLog 总是读 0?
self.requestURL = [NSURLRequest requestWithURL:[NSURL URLWithString:requestString]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
NSLog(@"request timeOutInterval:%d", self.requestURL.timeoutInterval); // always 0
接下来,我做这样的事情并且 timeoutInterval 没有设置。
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.
编辑。我现在正在使用 %f 记录 timeoutInterval 属性,并且两者都读取 20.000。但真正的问题是为什么我的 NSMutableURLRequest- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
在达到 timeoutInterval(20s) 时没有触发委托回调方法。相反,它仅在 75 秒左右超时。甚至比默认的 60 秒还要长...
即使我删除了[self.requestURL setTimeoutInterval:20];
线路,连接仍然会在 75 秒时超时。
我努力了
self.requestURL = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:requestString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0] autorelease];