NSMutableURLRequest timeout interval not taken into consideration for POST requests

感情迁移 提交于 2019-11-26 22:21:41

According to a post on the Apple developer forum, the minimum timeout interval for POST is 240 seconds. Any timeout interval shorter than that is ignored.

If you require a shorter timeout interval, use an async request along with a timer and call cancel on the NSURLConnection as needed.

link to thread: here

iOS 6 has fixed this issue.

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
user177844

Fixed with Clay Chambers's suggestion: with a custom timer Added a timer in a subclass of 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];

}

In the custom cancel method the connection is cancelled

[super cancel];     

It looks like problem described here is still facing iOS 7.1 (or reappeared). Fortunately it looks like setting timeoutIntervalForResource property on the configuration on the NSURLSession can fix this.

EDIT

According to @XiOS observations this works for timeouts shorter than (around) 2 minutes.

If you mean how to handle timeout error , I think no one answer this question yet

Let me write pice of my code to Explain that point

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

I hope this help with any one ask how to handle timeout error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!