好的,首先,如果我使用NSURLReuqest(非易变的),按照下面的设置,连接会超时。奇怪的是为什么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在到达timeoutInterval(20s)时没有触发- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error委托回调方法。取而代之的是,它只在75秒左右超时。甚至比默认的60更长...
即使我删除了[self.requestURL setTimeoutInterval:20];线路,连接仍然在75s超时。
我试过了
self.requestURL = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:requestString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0] autorelease];
发布于 2011-09-15 18:52:35
尝尝这个
NSLog(@"request timeOutInterval:%f", self.requestURL.timeoutInterval);它对我起作用了。
更新
在之前的SO question中,@Francois检查这个答案
在苹果开发论坛上有一个讨论这个问题的帖子。显然,在iPhone OS上,setter要求timeoutInterval至少为240秒(4分钟)。只有当postBody不为空时(通常在使用POST请求时),才会发生这种情况。这看起来很疯狂,但显然它是为了确保请求离开系统,即使WWAN (3G)接口可能需要几秒钟才能唤醒。240秒似乎相当陡峭,所以他们建议设置一个计时器,并在计时器触发时取消异步连接。我知道这看起来很愚蠢,但这是我唯一一次设法使POST请求超时...
发布于 2011-09-15 18:51:12
NSLog语句使用%d格式说明符,该说明符表示整数,但timeoutInterval属性是双精度型。
尝试使用%f,这表明您希望记录一个双精度值。
发布于 2011-09-15 18:51:53
使用%f,NSTimeInterval是浮点数。
Refer Foundation data types ref:
NSTimeInterval
Used to specify a time interval, in seconds.
typedef double NSTimeInterval;https://stackoverflow.com/questions/7429604
复制相似问题