0

我正在为我的项目使用 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 中的委托方法(即使我尝试在 [_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 "请求超时。"}

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

4

1 回答 1

0

试过这种方式吗?

RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://somewhere.com"];
objectManager.client.timeoutInterval = 600;
于 2012-07-16T19:23:11.500 回答