NSURLConnection GET最长60秒超时?

时间:2012-07-16 19:13:43

标签: objective-c timeout nsurlconnection

我正在为我的项目使用RestKit,我的请求超时需要10-20分钟(600 - 1200秒)。 我可以将超时从1秒更改为60秒,这可以毫无问题地工作。但是当我尝试设置超过60秒(90-9999999)时,我会在60秒后收到超时错误。 RKRequest.m中有这样的代码:

- (void)fireAsynchronousRequest {
    RKLogDebug(@"Sending asynchronous %@ request to URL %@.", [self HTTPMethod], [[self URL] absoluteString]);
    if (![self prepareURLRequest]) {
        // TODO: Logging
        return;
    }
    _isLoading = YES;

    if ([self.delegate respondsToSelector:@selector(requestDidStartLoad:)]) {
        [self.delegate requestDidStartLoad:self];
    }

    RKResponse* response = [[[RKResponse alloc] initWithRequest:self] autorelease];

    _connection = [[NSURLConnection connectionWithRequest:_URLRequest delegate:response] retain];


    [[NSNotificationCenter defaultCenter] postNotificationName:RKRequestSentNotification object:self userInfo:nil];
}

我在NSURLConnection init之前添加了一行:

[_URLRequest setTimeoutInterval:1200];

但是在60秒后再次收到超时错误。 RKResponse对象是NSURLconnection的委托。 所以我发现在60秒后调用RKResponce.m中的委托metod(即使我尝试在[_URLRequest setTimeoutInterval:1200]之后初始化NSURLConnection对象):

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    RKResponseIgnoreDelegateIfCancelled();
    _failureError = [error retain];
    NSLog(@"Error %@", error);
    [_request invalidateTimeoutTimer];
    [_request didFailLoadWithError:_failureError];
}

对我来说很奇怪,我不能超过NSURLConnection 60秒。

今天我创建了一个新项目来测试这个问题。我有这段代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *url = [NSURL URLWithString: @"http://5.9.10.68:8182"];
    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
    [urlRequest setHTTPMethod:@"GET"];
    [urlRequest setTimeoutInterval:300.0];
    NSLog(@"timeout %f",urlRequest.timeoutInterval);

    NSURLConnection*  urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate: self];

}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    NSLog(@"Error request %@", error);

}

所以这是日志:

2012-07-17 14:37:58.627 f [76799:f803]超时300.000000
2012-07-17 14:39:14.488 f [76799:f803]错误请求错误域= NSURLErrorDomain代码= -1001“请求超时。” UserInfo = 0x687a690 {NSErrorFailingURLStringKey = http://5.9.10.68:8182 /,NSErrorFailingURLKey = http://5.9.10.68:8182 /,NSLocalizedDescription =请求超时。,NSUnderlyingError = 0x687a070“请求超时。”} < / p>

所以超时= 75,但不是300.很奇怪。

1 个答案:

答案 0 :(得分:0)

试过这种方式?

RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://somewhere.com"];
objectManager.client.timeoutInterval = 600;