我正在尝试使用NDURL Download下载文件。为此,我必须登录到一个网站。我使用sendSynchronousRequest of NSURLConnection发送的NSMutableURLRequest完成此操作,从该消息调用中收到的数据实际上是确认我成功登录的html页面。要下载该文件,我使用以下代码:
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString: @"http://www.domain.com/getfile.php?file=1"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// Create the connection with the request and start loading the data.
NSURLDownload *theDownload = [[NSURLDownload alloc] initWithRequest:theRequest
delegate:self];
if (theDownload) {
// Set the destination file.
NSLog(@"Starting Download...");
NSLog(@"%@", [theDownload description]);
[theDownload setDestination:destinationFilename allowOverwrite:YES];
pathToZipFile = destinationFilename;
} else {
NSLog(@"Download failed...");
return nil;
}但我收到的数据是HTML页面,它告诉我必须登录才能下载文件。你对这个有什么想法吗?NSURLDownload的会话是否与NSURLConnection的会话不同?提前感谢!
发布于 2011-11-03 02:23:29
好了,你已经登录了,然后你尝试下载一个文件。但是服务器如何知道您是之前登录过的同一用户呢?
有不同的方法可以让它知道。一些cookie,一些请求参数,一些HTTP报头。但您必须在请求中添加一些内容,即“我是已立即登录的用户”。
发布于 2014-04-16 12:36:12
我觉得你必须为NSURLDownload实现委托,就像这样:
- (void)downloadDidBegin:(NSURLDownload *)download{
}
- (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response{
_totalLength = response.expectedContentLength;
}
- (void)download:(NSURLDownload *)download willResumeWithResponse:(NSURLResponse *)response fromByte:(long long)startingByte{
}
- (void)download:(NSURLDownload *)download didReceiveDataOfLength:(NSUInteger)length{
_recievedLength = _recievedLength + length;
}
- (void)downloadDidFinish:(NSURLDownload *)download{
//Completed//
}
- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error{
//Error
}https://stackoverflow.com/questions/7984936
复制相似问题