Downloading Large Google Drive files with WebClient in C#

回眸只為那壹抹淺笑 提交于 2019-12-13 18:30:39

问题


I know there are tones of questions on this subject already. After reading all the threads, I decided to get a redirected URL in a confirmation HTML page and then use it as a direct link to download.

As you know, the original URL format of the direct download link is like this.

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

But if the size of the target file is big, then it is like this.

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

I can get RRRR from the first downloaded data, so I need to try twice in order to download the real file. The concept is very simple enough but I can't get this to work.

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");
    }
}

What did I wrong? I can get the right download link from the first file, but I get another confirmation page file with another confirm code on the second try. I thought this was because of cookies, so I created my own WebClient class as you can see above.

Also I originally used DownloadFileAsync(), and changed it to DownloadFile() just in case, but the same result.. I'm still thinking it has something to do with cookie things.

What am I missing here?

来源:https://stackoverflow.com/questions/34323143/downloading-large-google-drive-files-with-webclient-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!