gpt4 book ai didi

c# - 如果文件大小超过 50MB,WebClient 不会完全下载文件

转载 作者:行者123 更新时间:2023-11-30 14:47:36 46 4
gpt4 key购买 nike

我正在使用 WebClient 下载文件。但是当它尝试下载刚好超过 50mb 的大文件时,有些可疑的事情正在发生,下载在 39kb 后立即完成并且直到最后才下载。任何人都知道可能是什么问题?

using (webClient = new WebClient())
{
webClient.Proxy = null;
webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);

// The variable that will be holding the url address (making sure it starts with http://)
Uri URL = urlAddress.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ? new Uri(urlAddress) : new Uri("http://" + urlAddress);

// Start the stopwatch which we will be using to calculate the download speed
sw.Start();

try
{
// Start downloading the file
webClient.DownloadFileAsync(URL, _mainWindow.outputDirComboBox.SelectedItem.ToString() + "\\" + _compressedClientFileDownloadRelativePath, new List<object>
{
_compressedClientFileDownloadRelativePath,
_decompressedClientFileDownloadRelativePath,
_lastDownloadedFilePath,
_compressedSize,
_lastCompressedFilePartRelativePath,
_filePartsRelativePath,
urlAddress,
_clientFilePack,
_clientFile
});
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

DownloadFileCompleted 仅检查错误和完成时间,但没有任何错误报告。只有它完成下载,就好像下载大小只有 39kb

编辑1:在挖掘时我最终来到这里:Downloading Large Google Drive files with WebClient in C#

看起来 Google Drive 提供了某种确认链接,但我还不知道如何将所有这些链接在一起工作。

编辑2:我刚刚确认,39kb是html页面确认

最佳答案

谢谢大家!

检查这个问题后: Downloading Large Google Drive files with WebClient in C#我搜索了使用 c# 从谷歌驱动器获取更大的文件并找到了 this :

using System;
using System.IO;
using System.Net;

public static class FileDownloader
{
private const string GOOGLE_DRIVE_DOMAIN = "drive.google.com";
private const string GOOGLE_DRIVE_DOMAIN2 = "https://drive.google.com";

// Normal example: FileDownloader.DownloadFileFromURLToPath( "http://example.com/file/download/link", @"C:\file.txt" );
// Drive example: FileDownloader.DownloadFileFromURLToPath( "http://drive.google.com/file/d/FILEID/view?usp=sharing", @"C:\file.txt" );
public static FileInfo DownloadFileFromURLToPath( string url, string path )
{
if( url.StartsWith( GOOGLE_DRIVE_DOMAIN ) || url.StartsWith( GOOGLE_DRIVE_DOMAIN2 ) )
return DownloadGoogleDriveFileFromURLToPath( url, path );
else
return DownloadFileFromURLToPath( url, path, null );
}

private static FileInfo DownloadFileFromURLToPath( string url, string path, WebClient webClient )
{
try
{
if( webClient == null )
{
using( webClient = new WebClient() )
{
webClient.DownloadFile( url, path );
return new FileInfo( path );
}
}
else
{
webClient.DownloadFile( url, path );
return new FileInfo( path );
}
}
catch( WebException )
{
return null;
}
}

// Downloading large files from Google Drive prompts a warning screen and
// requires manual confirmation. Consider that case and try to confirm the download automatically
// if warning prompt occurs
private static FileInfo DownloadGoogleDriveFileFromURLToPath( string url, string path )
{
// You can comment the statement below if the provided url is guaranteed to be in the following format:
// https://drive.google.com/uc?id=FILEID&export=download
url = GetGoogleDriveDownloadLinkFromUrl( url );

using( CookieAwareWebClient webClient = new CookieAwareWebClient() )
{
FileInfo downloadedFile;

// Sometimes Drive returns an NID cookie instead of a download_warning cookie at first attempt,
// but works in the second attempt
for( int i = 0; i < 2; i++ )
{
downloadedFile = DownloadFileFromURLToPath( url, path, webClient );
if( downloadedFile == null )
return null;

// Confirmation page is around 50KB, shouldn't be larger than 60KB
if( downloadedFile.Length > 60000 )
return downloadedFile;

// Downloaded file might be the confirmation page, check it
string content;
using( var reader = downloadedFile.OpenText() )
{
// Confirmation page starts with <!DOCTYPE html>, which can be preceeded by a newline
char[] header = new char[20];
int readCount = reader.ReadBlock( header, 0, 20 );
if( readCount < 20 || !( new string( header ).Contains( "<!DOCTYPE html>" ) ) )
return downloadedFile;

content = reader.ReadToEnd();
}

int linkIndex = content.LastIndexOf( "href=\"/uc?" );
if( linkIndex < 0 )
return downloadedFile;

linkIndex += 6;
int linkEnd = content.IndexOf( '"', linkIndex );
if( linkEnd < 0 )
return downloadedFile;

url = "https://drive.google.com" + content.Substring( linkIndex, linkEnd - linkIndex ).Replace( "&amp;", "&" );
}

downloadedFile = DownloadFileFromURLToPath( url, path, webClient );

return downloadedFile;
}
}

// Handles 3 kinds of links (they can be preceeded by https://):
// - drive.google.com/open?id=FILEID
// - drive.google.com/file/d/FILEID/view?usp=sharing
// - drive.google.com/uc?id=FILEID&export=download
public static string GetGoogleDriveDownloadLinkFromUrl( string url )
{
int index = url.IndexOf( "id=" );
int closingIndex;
if( index > 0 )
{
index += 3;
closingIndex = url.IndexOf( '&', index );
if( closingIndex < 0 )
closingIndex = url.Length;
}
else
{
index = url.IndexOf( "file/d/" );
if( index < 0 ) // url is not in any of the supported forms
return string.Empty;

index += 7;

closingIndex = url.IndexOf( '/', index );
if( closingIndex < 0 )
{
closingIndex = url.IndexOf( '?', index );
if( closingIndex < 0 )
closingIndex = url.Length;
}
}

return string.Format( "https://drive.google.com/uc?id={0}&export=download", url.Substring( index, closingIndex - index ) );
}
}

// Web client used for Google Drive
public class CookieAwareWebClient : WebClient
{
private class CookieContainer
{
Dictionary<string, string> _cookies;

public string this[Uri url]
{
get
{
string cookie;
if( _cookies.TryGetValue( url.Host, out cookie ) )
return cookie;

return null;
}
set
{
_cookies[url.Host] = value;
}
}

public CookieContainer()
{
_cookies = new Dictionary<string, string>();
}
}

private CookieContainer cookies;

public CookieAwareWebClient() : base()
{
cookies = new CookieContainer();
}

protected override WebRequest GetWebRequest( Uri address )
{
WebRequest request = base.GetWebRequest( address );

if( request is HttpWebRequest )
{
string cookie = cookies[address];
if( cookie != null )
( (HttpWebRequest) request ).Headers.Set( "cookie", cookie );
}

return request;
}

protected override WebResponse GetWebResponse( WebRequest request, IAsyncResult result )
{
WebResponse response = base.GetWebResponse( request, result );

string[] cookies = response.Headers.GetValues( "Set-Cookie" );
if( cookies != null && cookies.Length > 0 )
{
string cookie = "";
foreach( string c in cookies )
cookie += c;

this.cookies[response.ResponseUri] = cookie;
}

return response;
}

protected override WebResponse GetWebResponse( WebRequest request )
{
WebResponse response = base.GetWebResponse( request );

string[] cookies = response.Headers.GetValues( "Set-Cookie" );
if( cookies != null && cookies.Length > 0 )
{
string cookie = "";
foreach( string c in cookies )
cookie += c;

this.cookies[response.ResponseUri] = cookie;
}

return response;
}
}

有了那个 stackoverflow 问题 + 这个 github 代码,我知道它与这两个问题有关,因为谷歌在文件大于 50mb 时发送确认页面。因此,在实现该 github 代码后,我现在可以使用我的 webClient 下载大文件。

关于c# - 如果文件大小超过 50MB,WebClient 不会完全下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44401123/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com