ios POST 请求不考虑 NSMutableURLRequest 超时间隔
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1466389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSMutableURLRequest timeout interval not taken into consideration for POST requests
提问by user177844
I have the following problem. On a NSMutableURLRequest
using the HTTP
method POST
the timeout interval set for the connection is ignored. If the internet connection has a problem (wrong proxy, bad dns) the url request fails after about 2-4 minutes but not with NSLocalizedDescription = "timed out";
我有以下问题。在NSMutableURLRequest
使用该HTTP
方法时POST
,为连接设置的超时间隔将被忽略。如果互联网连接有问题(错误的代理,错误的 dns),url 请求会在大约 2-4 分钟后失败,但不会 NSLocalizedDescription = "timed out";
NSUnderlyingError = Error Domain=kCFErrorDomainCFNetwork Code=-1001 UserInfo=0x139580 "The request timed out.
If the http
method used is GET
it works fine.
The connection is async
over https
.
如果使用的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];
'
回答by user177844
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.
根据 Apple 开发者论坛上的帖子,POST 的最小超时间隔为 240 秒。任何比它短的超时间隔都将被忽略。
If you require a shorter timeout interval, use an async request along with a timer and call cancel on the NSURLConnection as needed.
如果您需要更短的超时间隔,请使用异步请求和计时器,并根据需要在 NSURLConnection 上调用取消。
link to thread: here
链接到线程:here
回答by freestyler
iOS 6 has fixed this issue.
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
回答by user177844
Fixed with Clay Chambers's suggestion: with a custom timer
Added a timer in a subclass of NSURLConnection
修正了 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];
}
In the custom cancel method the connection is cancelled
在自定义取消方法中,连接被取消
[super cancel];
回答by Julian Król
It looks like problem described here is still facing iOS 7.1 (or reappeared). Fortunately it looks like setting timeoutIntervalForResourceproperty on the configuration on the NSURLSession
can fix this.
看起来这里描述的问题仍然面临 iOS 7.1(或重新出现)。幸运的是,看起来像在配置上设置timeoutIntervalForResource属性NSURLSession
可以解决这个问题。
EDIT
编辑
According to @XiOS observations this works for timeouts shorter than (around) 2 minutes.
根据@XiOS 的观察,这适用于短于(大约)2 分钟的超时。
回答by Mina Fawzy
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
我希望这对任何询问如何处理超时错误的人有所帮助