2011-11-02 97 views
0

我想使用NDURL下載文件下載。爲此,我必須登錄到一個網站。 我這樣做使用NSMutableURLRequest,我發送使用sendSynchronousRequestNSURLConnection 我從該消息調用收到的數據確實是確認我成功登錄的html頁面。 要下載我用下面的代碼文件:登錄後(使用POST請求)下載文件(使用NSURLDownload)

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有不同的會話嗎? 在此先感謝!

回答

0

Okey,所以您已經登錄,然後嘗試下載文件。但是服務器如何知道你是之前登錄過的用戶?

有多種方式可以知道它。一些cookie,一些請求參數,一些HTTP頭。但是您必須在請求中添加一些內容,即「我是一分鐘前登錄的用戶」。

0

我覺得你必須實施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 
}