NSURLConnection 只下载前 567 个字节?
我正在尝试下载 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我用 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.