NSMutableURLRequest setTimeoutInterval 问题

发布于 2024-12-05 00:52:03 字数 1202 浏览 1 评论 0原文

好的,首先,如果我使用如下所示的 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]; 行,连接仍然在 75 秒时超时。

我尝试过

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仅一夜美梦 2024-12-12 00:52:03

试试这个

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

更新

检查@Francois在之前的SO问题

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

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...

半枫 2024-12-12 00:52:03

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

尝试使用 %f 代替,这表明您想要记录双精度值。

Your NSLog statement uses a %d format specifier, which indicates an integer, but the timeoutInterval property is a double.

Try using %f instead, which indicates you want to log a double value.

半暖夏伤 2024-12-12 00:52:03

它对我有用:

NSLog(@"request timeOutInterval:%@", requestURL.timeoutInterval);

it work for me:

NSLog(@"request timeOutInterval:%@", requestURL.timeoutInterval);
债姬 2024-12-12 00:52:03

使用%fNSTimeInterval是浮点数。

Refer Foundation data types ref:
NSTimeInterval
Used to specify a time interval, in seconds.
typedef double NSTimeInterval;

Use %f, NSTimeInterval is floating point number.

Refer Foundation data types ref:
NSTimeInterval
Used to specify a time interval, in seconds.
typedef double NSTimeInterval;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文