NSURLConnection 只下载前 567 个字节?

发布于 2024-09-24 19:42:21 字数 2681 浏览 3 评论 0原文

我正在尝试下载 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 左右)我认为它可能会开始解压缩并失败......

I'm trying to download http://www.vesseltracker.com/earth/vesseltrackerlight.kmz but am not getting all the bits and pieces.

I tried:

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

to no avail

then switched to

- (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];
}

But I am out of luck. It always takes 567 bytes (should be around 4k) I think it may start to decompress and fail....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隱形的亼 2024-10-01 19:42:21

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

I downloaded the URL you listed with Safari and it is only 567 bytes long. Are you basing your "4k" expectation on what the Finder list view says? That display is only approximate due to file allocation block sizes... The actual byte count of the file is displayed in parenthesis behind that value in the "Get Info..." window for the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文