【问题标题】:NSUrlconnection problem receiving data from some filehosts从某些文件主机接收数据的 NSUrlconnection 问题
【发布时间】:2011-06-05 21:58:54
【问题描述】:

我正在尝试开发一个下载管理器。我现在几乎可以在 linkclick 上的任何地方下载文件。

在 - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType 我检查 url 是否是二进制文件(如 zipfile)的 url。比我设置一个 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]]; }

在连接完成加载中,我关闭了文件。几乎所有主机都可以正常工作,除了主机之前有一个捕获程序,比如filedude.com 在 uiwebview 我可以浏览到下载页面输入验证码并获取下载链接。当我单击它时,将在带有文件名的文档目录中创建文件并开始下载,但他没有得到任何数据。每个文件都有 0kb 和 NSLog(@"content-length: %lli Bytes", expectedBytes);发出类似 100-400 字节的内容。

谁能帮我解决这个问题?

亲切的问候

【问题讨论】:

    标签: iphone objective-c iphone-sdk-3.0


    【解决方案1】:

    应该检查响应是什么

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

    【讨论】:

      【解决方案2】:

      看看ASIHttpRequest

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

      它非常灵活和可靠。只有当您有真正独特的需求时,最好自己编写。

      【讨论】:

      • 我也用 asihttprequest 试过了。同样的问题没有从filehosts之类的文件主机获取数据;(
      【解决方案3】:

      您是否可能没有遵循 HTTP 重定向?您是否尝试过捕获并查看您实际收到的数据?

      【讨论】:

      • 我收到一个空白页面。我试图在普通电脑上重建它。输入捕获后,您将获得下载链接。当您直接单击它时,下载开始但是当您复制链接并在浏览器的另一个选项卡或新窗口中打开它时,您只会得到一个没有任何内容的清晰页面。我认为同样的事情发生在应用程序上。有任何想法吗?以及如何检查我是否允许 http 重定向?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 2014-10-12
      • 2020-04-23
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      相关资源
      最近更新 更多