0
votes

i am trying to develop an downloadmanager. i can now download files from almost anywhere on linkclick.

in the - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType i check if the url is a url to a binaryfile like a zipfile. than i setup a nsurlconnection


    NSMutableURLRequest  *urlRequest = [NSMutableURLRequest requestWithURL:url
                                                               cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
                                                               timeoutInterval:20.0];
        [urlRequest setValue:@"User-Agent" forHTTPHeaderField:@"Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/418.9 (KHTML, like Gecko) Safari/419.3"];
        NSURLConnection *mainConnection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
        if (nil == mainConnection) {
            NSLog(@"Could not create the NSURLConnection object");
        }   

  • (void)connection:(NSURLConnection )connection didReceiveResponse:(NSURLResponse)response { self.tabBarController.selectedIndex=1; [receivedData setLength:0]; percent = 0; localFilename = [[[url2 absoluteString] lastPathComponent] copy]; NSLog(localFilename);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0] ;
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:localFilename];
    [[NSFileManager defaultManager] createFileAtPath:appFile contents:nil attributes:nil]; [downloadname setHidden:NO]; [downloadname setText:localFilename]; expectedBytes = [response expectedContentLength]; exp = [response expectedContentLength]; NSLog(@"content-length: %lli Bytes", expectedBytes); file = [[NSFileHandle fileHandleForUpdatingAtPath:appFile] retain]; if (file) {

    [file seekToEndOfFile];
    

    } }

  • (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { if (file) { [file seekToEndOfFile]; } [file writeData:data]; [receivedData appendData:data]; long long resourceLength = [receivedData length];
    float res = [receivedData length]; percent = res/exp; [progress setHidden:NO]; [progress setProgress:percent]; NSLog(@"Remaining: %lli KB", (expectedBytes-resourceLength)/1024); [kbleft setHidden:NO]; [kbleft setText:[NSString stringWithFormat:@"%lli / %lli KB", expectedBytes/1024 ,(resourceLength)/1024]]; }

in the connectiondidfinish loading i close the file. all working fine for nearly every hoster except hosters wich have a capture procedure before like filedude.com in the uiwebview i can surf to the downloadpage enter the captcha and get the downloadlink. when i click on it the file will be created in the documentsdir with the filename and the download starts but he dont get any data. every file has 0kb and the NSLog(@"content-length: %lli Bytes", expectedBytes); gives out something like 100-400 byte .

can somebody help me solve this problem?

kind regards

3

3 Answers

0
votes

Should check what the response is

NSLog(@"%@", [connection allHttpHeaderFields])
0
votes

Take a look at ASIHttpRequest

http://allseeing-i.com/ASIHTTPRequest/

It is very flexible and reliable. Only if you have truly unique requirements is it a better idea to write your own.

0
votes

Is it possible that you're not following an HTTP redirect? Have you tried capturing and looking at the data you're actually receiving?