我有以下问题。 上的NSMutableURLRequest
使用HTTP
方法POST
超时间隔设定为连接被忽略。 如果网络连接有问题(错误的代理,坏DNS)约2-4分钟后URL请求失败,但是不能与NSLocalizedDescription = "timed out";
NSUnderlyingError = Error Domain=kCFErrorDomainCFNetwork Code=-1001 UserInfo=0x139580 "The request timed out.
如果http
使用的方法是GET
,它工作正常。 连接是async
过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];
'
根据对苹果开发者论坛一个帖子,对POST最小超时间隔为240秒。 比任何较短的超时间隔将被忽略。
如果您需要更短的超时时间间隔,使用异步请求与定时器一起,并呼吁根据需要在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
修正了克莱钱伯斯的建议:用自定义定时器的子类中增加了一个计时器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(或重新出现)。 幸好它看起来像设置timeoutIntervalForResource上的配置属性NSURLSession
可以解决这个问题。
编辑
根据@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");
}
我希望这帮助任何一个询问如何处理超时错误
文章来源: NSMutableURLRequest timeout interval not taken into consideration for POST requests