POST请求不考虑NSMutableURLRequest超时间隔

我有以下问题。 在使用HTTP方法POSTNSMutableURLRequest ,为连接设置的超时间隔被忽略。 如果互联网连接有问题(代理错误,错误的DNS),约2-4分钟后url请求失败,但NSLocalizedDescription = "timed out";不会NSLocalizedDescription = "timed out";

 NSUnderlyingError = Error Domain=kCFErrorDomainCFNetwork Code=-1001 UserInfo=0x139580 "The request timed out. 

如果使用的http方法是GET它工作正常。 通过https连接是async

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

根据苹果开发者论坛上的post,POST的最小超时间隔是240秒。 任何短于此的超时间隔都将被忽略。

如果您需要更短的超时间隔,则根据需要使用asynchronous请求和定时器,并调用NSURLConnection上的取消。

链接到线程: 在这里

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 

修正了Clay Chambers的build议:用自定义定时器在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]; 

看起来像这里描述的问题仍然面临iOS 7.1(或重新出现)。 幸运的是,在NSURLSession上的configuration上设置timeoutIntervalForResource属性可以解决这个问题。

编辑

根据@XiOS的观察,这适用于比(大约)2分钟更短的超时。

如果你的意思是如何处理超时错误,我认为没有人回答这个问题呢

让我写我的代码pice来解释这一点

 // replace "arg" with your argument you want send to your site NSString * param = [NSString stringWithFormat:@"arg"]; NSData *Postdata = [param dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[Postdata length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init]; // replace "yoursite" with url of site you want post to [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http//yoursite"]]]; // set what ever time you want in seconds 120 mean 2 min , 240 mean 4 min [request setTimeoutInterval:120]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"]; [request setHTTPBody:Postdata]; NSURLConnection * connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; // if request time out without response from server error will occurred -(void) connection:(NSURLConnection * ) connection didFailWithError:(NSError *)error { if (error.code == NSURLErrorTimedOut) // handle error as you want NSLog(@"Request time out , Please try again later"); } 

我希望这有助于任何一个问如何处理超时错误