POST请求未考虑NSMutableURLRequest超时间隔

时间:2009-09-23 14:33:50

标签: ios objective-c post nsurlconnection

我有以下问题。在使用NSMutableURLRequest方法HTTP的{​​{1}}上,将忽略为连接设置的超时间隔。如果互联网连接出现问题(代理错误,dns错误),则网址请求在大约2-4分钟后失败,但不会导致POST

NSLocalizedDescription = "timed out";

如果使用的NSUnderlyingError = Error Domain=kCFErrorDomainCFNetwork Code=-1001 UserInfo=0x139580 "The request timed out. 方法是http,则可以正常使用。    连接GET超过async

https

5 个答案:

答案 0 :(得分:28)

根据Apple开发人员论坛上的帖子,POST的最短超时间隔为240秒。忽略任何短于此的超时间隔。

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

链接到主题:here

答案 1 :(得分:27)

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

答案 2 :(得分:10)

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

答案 3 :(得分:3)

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

修改

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

答案 4 :(得分:0)

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

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

// 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");
}

我希望对任何一个人有帮助,询问如何处理超时错误