Uploading large Google Drive files with WebClient in C #

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

As you know, the original format of the direct download URL is as follows.

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

But if the target file size is large, then it looks like this.

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

I can get the RRRR from the first data loaded, so I have to try twice to load the real file. The concept is very simple, but I can't seem to 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 have I misunderstood? I can get the correct download link from the first file, but on the second try I get another confirmation file with a different confirmation code. I thought it was because of the cookies, so I created my own class WebClient

as you can see above.

Also I originally used DownloadFileAsync()

and just in case changed it to DownloadFile()

, but same result .. I still think it has something to do with the cookie stuff.

What am I missing here?

+1


source to share





All Articles