Last active
December 16, 2015 04:38
-
-
Save goooseman/5378021 to your computer and use it in GitHub Desktop.
This code downloads a .torrent file from Russian torrent tracker rutracker.org, but this is a good example of downloading a file, which needs cookies and a "referer" header with NSURLConnection and NSData.
All cookies are saved/loaded to/from system storage (that is used by Safari).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// RutrackerSimpleDownload | |
// Created by Alexander Gusev on 12.04.13. | |
// Copyright (c) 2013 Alexander Gusev. All rights reserved. | |
// http://goooseman.ru | |
// Variables // | |
NSString *username = @""; | |
NSString *password = @""; | |
NSInteger fileID = 4296614; | |
NSString *fileLocation = @"/Users/goooseman/Downloads/"; //should be /path/to/file/ not /path/to/file. ~ doesn't work here | |
// Login Session (Getting cookies) // | |
NSURL *loginUrl = [NSURL URLWithString:@"http://login.rutracker.org/forum/login.php"]; | |
NSMutableURLRequest *loginRequest = [NSMutableURLRequest requestWithURL:loginUrl]; | |
NSHTTPURLResponse *loginResponse; | |
NSError* loginError; | |
NSString *post = [NSString stringWithFormat:@"login_username=%@&login_password=%@&login=Вход", username, password]; | |
NSLog(@"%@", post); | |
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; | |
[loginRequest setValue:[NSString stringWithFormat:@"%ld",[postData length]] forHTTPHeaderField:@"Content-Length"]; | |
[loginRequest setTimeoutInterval:15]; | |
[loginRequest setHTTPBody:postData]; | |
[loginRequest setHTTPMethod:@"POST"]; | |
[loginRequest setHTTPShouldHandleCookies:YES]; | |
[NSURLConnection sendSynchronousRequest:loginRequest returningResponse:&loginResponse error:&loginError]; | |
// Download file // | |
NSURL *torrentUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://dl.rutracker.org/forum/dl.php?t=%ld", fileID]]; | |
NSLog(@"%@", torrentUrl); | |
NSHTTPURLResponse *torrentResponse; | |
NSError *torrentError; | |
NSMutableURLRequest *torrentRequest = [NSMutableURLRequest requestWithURL:torrentUrl]; | |
NSString *torrentPost = [NSString stringWithFormat:@"t=%ld", fileID]; | |
NSData *torrentPostData = [torrentPost dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; | |
[torrentRequest setValue:[NSString stringWithFormat:@"%ld",[postData length]] forHTTPHeaderField:@"Content-Length"]; | |
[torrentRequest setHTTPShouldHandleCookies:YES]; | |
[torrentRequest setHTTPBody:torrentPostData]; | |
[torrentRequest setHTTPMethod:@"POST"]; | |
[torrentRequest setValue:[NSString stringWithFormat:@"t:%ld", fileID] forHTTPHeaderField:@"header"]; | |
[torrentRequest setValue:[NSString stringWithFormat:@"http://rutracker.org/forum/viewtopic.php?t=%ld", fileID] forHTTPHeaderField:@"Referer"]; | |
NSData *torrentUrlConnection = [NSURLConnection sendSynchronousRequest:torrentRequest returningResponse:&torrentResponse error:&torrentError]; | |
[torrentUrlConnection writeToFile:[NSString stringWithFormat:@"%@[rutracker.org].t%ld.torrent", fileLocation, fileID] atomically:YES]; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment