32

我有以下问题。在NSMutableURLRequest使用该HTTP方法POST时,为连接设置的超时间隔将被忽略。如果互联网连接有问题(错误的代理、错误的 dns),则 url 请求会在大约 2-4 分钟后失败,但不会 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];

'
4

5 回答 5

28

根据 Apple 开发者论坛上的帖子,POST 的最小超时间隔为 240 秒。任何比这更短的超时间隔都会被忽略。

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

线程链接:这里

于 2009-11-19T18:36:17.647 回答
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
于 2012-10-15T17:45:48.783 回答
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];     
于 2010-05-14T15:13:58.430 回答
3

看起来这里描述的问题仍然面临 iOS 7.1(或再次出现)。幸运的是,它看起来像在配置上设置timeoutIntervalForResourceNSURLSession属性可以解决这个问题。

编辑

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

于 2014-10-29T09:56:54.430 回答
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");
}

我希望这对任何询问如何处理超时错误的人有所帮助

于 2015-01-15T12:02:23.943 回答