代码之家  ›  专栏  ›  技术社区  ›  Shaik Riyaz

NSMutableURLRequest“请求超时”问题。

  •  3
  • Shaik Riyaz  · 技术社区  · 11 年前

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

    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."}
    

    那么,有人能建议我如何解决这个问题吗。

    3 回复  |  直到 11 年前
        1
  •  4
  •   user3061811 user3061811    11 年前

    将超时间隔增加到150秒。

        2
  •  1
  •   Master Stroke    11 年前

    尝试使用名为 [NSURLConnection sendAsynchronousRequest:queue:completionHandler: :

    例子 :

    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
  •  0
  •   thatzprem    11 年前

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