用C#中的WebClient下载大型Google Drive文件

Downloading Large Google Drive files with WebClient in C#(用C#中的WebClient下载大型Google Drive文件)

本文介绍了用C#中的WebClient下载大型Google Drive文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道已经有关于这个主题的各种问题了。在阅读完所有帖子后,我决定在确认的HTML页面中获得一个重定向的URL,然后将其用作下载的直接链接。

如您所知,直接下载链接的原始URL格式如下所示。

https://drive.google.com/uc?export=download&id=XXXXX..

但如果目标文件的大小很大,则如下所示。

https://drive.google.com/uc?export=download&confirm=RRRR&id=XXXXX..

我可以从第一次下载的数据中获得RRRR,所以我需要尝试两次才能下载真正的文件。概念很简单,但我不能让它起作用。

class Test
{
    class MyWebClient: WebClient
    {
        CookieContainer c = new CookieContainer();

        protected override WebRequest GetWebRequest(Uri u)
        {
            var r = (HttpWebRequest) base.GetWebRequest(u);
            r.CookieContainer = c;
            return r;
        }
    }

    static string GetRealURL(string filename)
    {
        // Some Jobs to Parse....
        return directLink;
    }

    static void Main()
    {
        MyWebClient wc = new MyWebClient();

        string targetLink = "https://drive.google.com/uc?export=download&id=XXXXXXX";
        wc.DownloadFile(targetLink, "tempFile.tmp");

        targetLink = GetRealURL("tempFile.tmp");
        wc.DownloadFile(targetLink, "realFile.dat");
    }
}

我做错了什么? 我可以从第一个文件中获得正确的下载链接,但我在第二次尝试时得到了另一个带有另一个确认码的确认页面文件。我认为这是因为Cookie,所以我创建了我自己的WebClient类,正如您在上面看到的。

另外,我最初使用DownloadFileAsync(),并将其改为DownloadFile()以防万一,但结果相同.. 我仍然认为这与曲奇的事情有关。

我在这里遗漏了什么?

推荐答案

我也遇到过同样的问题,但已经在HttpClient中解决了它。我通过您的WebClient方法进行了尝试,并使其发挥了作用。您没有显示您的GetRealUrl()来源,但我愿意打赌这就是问题所在。我是这样做的:

  • 您需要解析html响应以获取";无论如何下载&按钮的href属性中的url。它将只有相对URL(/uc?export=download...部分)
  • 您需要将XML转义字符&替换为&
  • 然后您可以使用域https://drive.google.com
  • 构建URL

此时您可以下载该文件。以下是源代码(在测试WPF应用程序中使用):

class MyWebClient : WebClient
{
    CookieContainer c = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri u)
    {
        var r = (HttpWebRequest)base.GetWebRequest(u);
        r.CookieContainer = c;
        return r;
    }
}

private async void WebClientTestButtonGdrive_Click(object sender, RoutedEventArgs e)
{
    using (MyWebClient client = new MyWebClient())
    {
        //get the warning page
        string htmlPage = await client.DownloadStringTaskAsync("https://drive.google.com/uc?id=XXXXXXX&export=download");

        //use HtmlAgilityPack to get the url with the confirm parameter in the url
        HtmlDocument document = new HtmlDocument();
        document.LoadHtml(htmlPage);
        HtmlNode node = document.DocumentNode;
        HtmlNode urlNode = node.SelectSingleNode(@"//a[contains(@href, 'XXXXXXX') and contains(@id, 'uc-download-link')]//@href");
        string downloadUrl = urlNode.Attributes["href"].Value;
        downloadUrl = downloadUrl.Replace("&", "&");
        downloadUrl = "https://drive.google.com" + downloadUrl;

        //download the file
        if (File.Exists("FileToDownload.zip"))
            File.Delete("FileToDownload.zip");
        await client.DownloadFileTaskAsync(downloadUrl, "FileToDownload.zip");
    }
}

这篇关于用C#中的WebClient下载大型Google Drive文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:用C#中的WebClient下载大型Google Drive文件

基础教程推荐