我有以下问题。在使用NSMutableURLRequest方法POST的HTTP上,将忽略为连接设置的超时间隔。如果internet连接有问题(错误的代理,错误的dns),url请求在大约2-4分钟后就会失败,但在NSLocalizedDescription = "timed out";中不会失败。
NSUnderlyingError = Error Domain=kCFErrorDomainCFNetwork Code=-1001 UserInfo=0x139580 "The request timed out.如果使用的http方法是GET,那么它工作得很好。连接是async over https。
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setTimeoutInterval:10];
//Set the request method to post
[request setHTTPMethod:@"POST"];
if (body != nil) {
[request setHTTPBody:body];
}
// Add general parameters to the request
if (authorization){
[request addValue: authorization forHTTPHeaderField:@"Authorization"];
}
[request addValue: WS_HOST forHTTPHeaderField:@"Host"];
[request addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[self addToQueueRequest:request withDelegate:delegate];
'发布于 2009-11-19 18:36:17
根据Apple developer论坛上的一篇文章,帖子的最低超时间隔为240秒。任何短于此的超时间隔都会被忽略。
如果需要更短的超时间隔,请使用异步请求和计时器,并根据需要在NSURLConnection上调用cancel。
链接到线程:这里
发布于 2012-10-15 17:45:48
iOS 6已经解决了这个问题。
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20];
[request setHTTPMethod:method];
[request setHTTPBody:requestBody];
NSLog(@"%f", [request timeoutInterval]);
//20.0 in iOS 6
//240.0 in iOS 4.3, 5.0, 5.1发布于 2010-05-14 15:13:58
修正了克莱钱伯斯的建议:使用定制计时器在NSURLConnection子类中添加一个计时器
if (needsSeparateTimeout){
SEL sel = @selector(customCancel);
NSMethodSignature* sig = [self methodSignatureForSelector:sel];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setTarget:self];
[invocation setSelector:sel];
NSTimer *timer = [NSTimer timerWithTimeInterval:WS_REQUEST_TIMEOUT_INTERVAL invocation:invocation repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}在自定义取消方法中,连接被取消。
[super cancel]; https://stackoverflow.com/questions/1466389
复制相似问题