首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >登录后下载文件(使用NSURLDownload) (使用POST请求)

登录后下载文件(使用NSURLDownload) (使用POST请求)
EN

Stack Overflow用户
提问于 2011-11-03 01:48:20
回答 2查看 728关注 0票数 0

我正在尝试使用NDURL Download下载文件。为此,我必须登录到一个网站。我使用sendSynchronousRequest of NSURLConnection发送的NSMutableURLRequest完成此操作,从该消息调用中收到的数据实际上是确认我成功登录的html页面。要下载该文件,我使用以下代码:

代码语言:javascript
复制
 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的会话不同?提前感谢!

EN

回答 2

Stack Overflow用户

发布于 2011-11-03 02:23:29

好了,你已经登录了,然后你尝试下载一个文件。但是服务器如何知道您是之前登录过的同一用户呢?

有不同的方法可以让它知道。一些cookie,一些请求参数,一些HTTP报头。但您必须在请求中添加一些内容,即“我是已立即登录的用户”。

票数 0
EN

Stack Overflow用户

发布于 2014-04-16 12:36:12

我觉得你必须为NSURLDownload实现委托,就像这样:

代码语言:javascript
复制
- (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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7984936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档