我需要用UIRequest设置超时15秒或30秒,但它总是使用默认值。有没有办法设置连接的最小超时。
发布于 2012-07-30 20:51:25
This answer解释了NSURLRequest对象的timeoutInterval的最小值。如果您需要一个较小的值,那么您可以在所需的时间启动一个NSTimer,并且在timer的触发方法中,您可以取消NSURLConnection对象的连接。如下所示:
//....
connection = [[NSURLConnection connectionWithRequest:request delegate:self] retain];
[request release];
[connection start];
if (timer == NULL) {
timer = [NSTimer scheduledTimerWithTimeInterval: TimeOutSecond
target: self
selector: @selector(cancelURLConnection:)
userInfo: nil
repeats: NO];
[timer retain];
}
- (void)cancelURLConnection:(NSTimer *)timerP {
[connection cancel]; //NSURLConnection object
NSLog(@"Connection timeout.");
[timer invalidate];
}发布于 2012-07-30 17:09:11
在构造时设置超时间隔属性似乎有问题:
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:240.0];而是在构造之后设置它:
request.timeoutInterval = 70;
另请注意,您可以将间隔设置得多低似乎存在一些限制。有关更多信息,请阅读此帖子:https://devforums.apple.com/message/108087#108087
发布于 2012-07-30 17:27:26
POST请求有一个最小的超时时间,我相信是4分钟。最安全的方法是启动一个NSTimer,并在超时触发时取消请求。
https://stackoverflow.com/questions/11718256
复制相似问题