2011-01-08 71 views
0

我正在嘗試開發一個下載管理器。我現在可以從幾乎任何地方通過linkclick下載文件。從某些文件主機接收數據的NSUrlconnection問題

在 - (BOOL)web視圖:(的UIWebView *)webView的shouldStartLoadWithRequest:(的NSURLRequest *)請求navigationType:(UIWebViewNavigationType)navigationType 我檢查如果URL是一個URL到像zip文件一個binaryfile。比我設置一個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]; 
    

    } }

  • (無效)連接:(NSURLConnection的*)連接didReceiveData:(NSData的*)數據{ 如果(文件){ [文件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]]; }

在connectiondidfinish裝載

我關閉文件。所有工作正常幾乎所有的主機除了主機之外,其中有一個捕獲程序之前像filedude.com 在uiwebview我可以衝浪到下載頁面輸入驗證碼,並獲得下載鏈接。當我點擊它時,將在文件目錄中創建文件,文件名和下載開始,但他沒有得到任何數據。每個文件都有0kb和NSLog(@「content-length:%lli Bytes」,expectedBytes);給出了100-400字節之類的東西。

有人可以幫我解決這個問題嗎?

親切的問候

回答

0

應檢查的反應是什麼

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

到其他filehosters沒有什麼區別;( – Tammo 2011-01-09 23:31:13

0

看看ASIHttpRequest

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

它非常靈活,可靠。只有你有真正獨特的要求,編寫你自己的要求才是更好的主意。

+0

我已經與asihttprequest也嘗試過同樣的問題,不從像filedude filehosts獲取數據;( – Tammo 2011-01-09 00:23:23

0

您是否有可能沒有遵循HTTP重定向?您是否嘗試捕獲並查看實際收到的數據?

+0

我收到一個空頁我已經試圖重建它在普通PC進入後。捕獲你得到的下載鏈接。當你直接點擊它,下載開始,但是當你複製鏈接並在瀏覽器或新窗口的另一個選項卡中打開它時,你只會得到一個沒有任何內容的清晰頁面。我認爲同樣的事情發生在應用程序上。有任何想法嗎?以及如何檢查我是否允許http重定向? – Tammo 2011-01-09 14:14:03