【问题标题】:NSMutableURLRequest "The request timed out" issue . .NSMutableURLRequest“请求超时”问题。 .
【发布时间】:2013-09-05 09:15:04
【问题描述】:

我正在尝试使用以下代码在我的应用程序中下载数据

NSURL *url = [NSURL URLWithString:@"my download url string"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.f];

NSURLConnection * connection = [NSURLConnection connectionWithRequest:request delegate:self];

[connection start];

但问题是有时我收到以下错误

Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x1d5be240 
{NSErrorFailingURLStringKey=http://dr282zn36sxxg.cloudfront.net/datastreams/f-
d%3Afc7f649e1e3ba58452f67e3fa1f66f69a15b96b3ea585c946e4fa854%2BEPUB%2BEPUB.1, 
NSErrorFailingURLKey=http://dr282zn36sxxg.cloudfront.net/datastreams/f-
d%3Afc7f649e1e3ba58452f67e3fa1f66f69a15b96b3ea585c946e4fa854%2BEPUB%2BEPUB.1, 
NSLocalizedDescription=The request timed out., NSUnderlyingError=0x1e1975b0 "The request 
timed out."}

所以,任何人都可以建议我如何解决这个问题。 . .

【问题讨论】:

  • 您要下载什么样的数据?
  • @iOSCoder 看看错误信息 ;)
  • @iOSCoder 是扩展名为 .epub 的文件
  • 我刚刚在模拟器中试了一下,确实可以用
  • @riyaz 文件很大,你是用 Wifi 还是手机下载?

标签: ios objective-c nsurlconnection nsmutableurlrequest nsurlconnectiondelegate


【解决方案1】:

将超时间隔增加 150 秒。

【讨论】:

    【解决方案2】:

    尝试使用名为 [NSURLConnection sendAsynchronousRequest:queue:completionHandler: 的 GCD 抽象:

    示例

    NSURL *url = [NSURL URLWithString:@"your_URL"];
    NSURLRequest *myUrlRequest = [NSURLRequest requestWithURL:url];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest: myUrlRequest queue: queue completionHandler: ^ (NSURLResponse *response, NSData *data, NSError *error)
    {
    
        if ([data length] > 0 && error == nil)
            //doSomething With The data
    
        else if (error != nil && error.code == ERROR_CODE_TIMEOUT)
            //time out error
    
        else if (error != nil)
            //download error
    }];
    

    有什么不同吗?

    【讨论】:

      【解决方案3】:

      错误消息清楚地表明请求已超时 - 到达一个您可以获得更好的网络速度或增加请求超时的地方,我现在有时看到它为 60,这可能不足以下载图像。

      【讨论】:

      • 不是图片下载 ;)
      • 在当前的 NSURLConnection 实现中,当连接有 "timeoutInterval" 秒 inactivity 时会发生超时。也就是说,即使数据进入缓慢,当您每“timeoutInterval”秒(60 秒,如指定)至少获得一个字节时,它也不应该超时。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-22
      • 1970-01-01
      • 2021-06-22
      • 2019-09-08
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多