【问题标题】:NSURLConnection downloads only the first 567 bytes?NSURLConnection 只下载前 567 个字节?
【发布时间】:2010-09-27 19:38:37
【问题描述】:

我正在尝试下载http://www.vesseltracker.com/earth/vesseltrackerlight.kmz,但没有得到所有的点点滴滴。

我试过了:

  NSData *data = [NSData dataWithContentsOfURL: serverURL options: 0 error: &error];

无济于事

然后切换到

- (void)startDownloadingURL:(NSURL*) url
{
    // Create the request.
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url
            cachePolicy:NSURLRequestUseProtocolCachePolicy
           timeoutInterval:60.0];

    // create the connection with the request
 // and start loading the data
 NSLog(@"SNNetworkController.startDownloadingURL [%@]", url);
 NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
 if (theConnection) {
  // Create the NSMutableData to hold the received data.
  // receivedData is an instance variable declared elsewhere.
  receivedData = [[NSMutableData data] retain];
    } else {
        // inform the user that the download failed.
  NSLog(@"SNNetworkController.startDownloadingURL Download failed!");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // This method is called when the server has determined that it
    // has enough information to create the NSURLResponse.

    // It can be called multiple times, for example in the case of a
    // redirect, so each time we reset the data.

    // receivedData is an instance variable declared elsewhere.
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to receivedData.
    // receivedData is an instance variable declared elsewhere.
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    // release the connection, and the data object
    [connection release];
    // receivedData is declared as a method instance elsewhere
    [receivedData release];

    // inform the user
    NSLog(@"SNNetworkController.didFailWithError Download failed! Error - %@",
          [error localizedDescription]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data
    // receivedData is declared as a method instance elsewhere
    NSLog(@"SNNetworkController.downloadDidFinish Succeeded! Received %d bytes of data",[receivedData length]);

    // release the connection, and the data object
    [connection release];
    [receivedData release];
}

但我运气不好。它总是需要 567 个字节(应该在 4k 左右)我认为它可能会开始解压缩并失败....

【问题讨论】:

    标签: cocoa nsurlconnection nsurl kmz


    【解决方案1】:

    我下载了您在 Safari 中列出的 URL,它只有 567 字节长。您是否将您的“4k”期望基于 Finder 列表视图的内容?由于文件分配块大小,该显示只是近似值...文件的实际字节数显示在文件的“获取信息...”窗口中该值后面的括号中。

    【讨论】:

    • 我用 curl 下载了它并得到了相同的结果,所以它不是特定于 WebKit-/Foundation 的;看来服务器提供的文件实际上只有 567 字节。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 2023-03-12
    • 1970-01-01
    • 2011-06-24
    • 2011-09-07
    相关资源
    最近更新 更多