NSMutableURLRequest setTimeoutInterval 问题
好的,首先,如果我使用如下所示的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个
NSLog(@"request timeOutInterval:%f", self.requestURL.timeoutInterval);
它对我有用。更新
检查@Francois在之前的SO问题
Try this
NSLog(@"request timeOutInterval:%f", self.requestURL.timeoutInterval);
It worked for me.UPDATE
Check this answer by @Francois in this previous SO question
您的
NSLog
语句使用%d
格式说明符,它表示整数,但timeoutInterval
属性是双精度型。尝试使用
%f
代替,这表明您想要记录双精度值。Your
NSLog
statement uses a%d
format specifier, which indicates an integer, but thetimeoutInterval
property is a double.Try using
%f
instead, which indicates you want to log a double value.它对我有用:
it work for me:
使用
%f
,NSTimeInterval
是浮点数。Use
%f
,NSTimeInterval
is floating point number.