1

好的,首先,如果我如下使用 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];

4

4 回答 4

3

尝试这个

NSLog(@"request timeOutInterval:%f", self.requestURL.timeoutInterval);它对我有用。

更新

在上一个SO 问题中检查@Francois 的答案

Apple 开发者论坛上有一个讨论这个问题的帖子。显然在 iPhone OS 上,setter 要求 timeoutInterval 至少为 240 秒(4 分钟)。这只发生在 postBody 不为空时(通常在使用 POST 请求时)。这看起来很疯狂,但显然它的存在是为了确保请求离开系统,即使 WWAN (3G) 接口可能需要几秒钟才能唤醒。240 秒似乎相当陡峭,因此他们建议设置一个计时器并在计时器触发时取消异步连接。我知道这看起来很愚蠢,但这是我唯一设法让 POST 请求超时......

于 2011-09-15T10:52:35.263 回答
0

您的NSLog语句使用%d格式说明符,它表示整数,但timeoutInterval属性是双精度数。

尝试%f改用,这表明您要记录双精度值。

于 2011-09-15T10:51:11.770 回答
0

使用%f,NSTimeInterval是浮点数。

Refer Foundation data types ref:
NSTimeInterval
Used to specify a time interval, in seconds.
typedef double NSTimeInterval;
于 2011-09-15T10:51:52.867 回答
0

它对我有用:

NSLog(@"request timeOutInterval:%@", requestURL.timeoutInterval);
于 2011-09-15T10:56:31.063 回答