C# 如果文件大小超过50MB,WebClient无法完全下载文件
我正在使用WebClient下载文件。 但当它试图下载大于50mb的更大文件时,有些可疑,39kb后下载就完成了,直到最后才下载。 有人知道会是什么问题吗C# 如果文件大小超过50MB,WebClient无法完全下载文件,c#,webclient,C#,Webclient,我正在使用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 (KHT
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);
}
}
使用(webClient=newWebClient())
{
webClient.Proxy=null;
webClient.Headers.Add(“用户代理”、“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)”);
webClient.DownloadFileCompleted+=新的AsyncCompletedEventHandler(已完成);
webClient.DownloadProgressChanged+=新的DownloadProgressChangedEventHandler(ProgressChanged);
//将保存url地址的变量(确保它以http://开头)
Uri URL=urlAddress.StartWith(“http://”,StringComparison.OrdinalIgnoreCase)?新Uri(urlAddress):新Uri(“http://”+urlAddress);
//启动秒表,我们将使用它来计算下载速度
sw.Start();
尝试
{
//开始下载文件
webClient.DownloadFileAsync(URL,\u mainWindow.outputDirComboBox.SelectedItem.ToString()+“\\”+\u compressedClientFileDownloadRelativePath,新列表
{
_压缩客户端文件下载相对路径,
_解压缩的客户端文件下载RelativePath,
_上次下载的文件路径,
_压缩,
_最后一次压缩零件相对尺寸,
_filePartsRelativePath,
URL地址,
_clientFilePack,
_客户端文件
});
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
DownloadFileCompleted只检查错误和完成时间,但没有任何错误报告。只是它完成了下载,就像下载大小只有39kb一样
Edit1:
在挖掘过程中,我在这里结束:
看起来Google Drive提供了某种确认链接,但我还不知道如何将所有内容组合在一起工作
Edit2:
我刚刚确认,39kb是html页面确认您必须等待webClient.DownloadFileAsync进程 否则,当文件仍在下载时,您的执行将下降到dispose命令 例如:
public class DownloadManager
{
public void DownloadFile(string sourceUrl, string targetFolder)
{
WebClient downloader = new WebClient();
// fake as if you are a browser making the request.
downloader.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)");
downloader.DownloadFileCompleted += new AsyncCompletedEventHandler(Downloader_DownloadFileCompleted);
downloader.DownloadFileAsync(new Uri(sourceUrl), targetFolder);
}
private void Downloader_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
// display completion status.
if (e.Error != null)
Console.WriteLine(e.Error.Message);
else
Console.WriteLine("Download Completed!!!");
}
}
谢谢大家
检查此问题后:
我用c#从google drive搜索更大的文件,发现:
使用系统;
使用System.IO;
Net系统;
公共静态类文件下载程序
{
私有常量字符串GOOGLE\u DRIVE\u DOMAIN=“DRIVE.GOOGLE.com”;
私有常量字符串GOOGLE\u DRIVE\u DOMAIN2=”https://drive.google.com";
//普通示例:FileDownloader.DownloadFileFromURLToPath(“http://example.com/file/download/link“,@“C:\file.txt”);
//驱动器示例:FileDownloader.DownloadFileFromURLToPath(“http://drive.google.com/file/d/FILEID/view?usp=sharing“,@“C:\file.txt”);
公共静态文件信息下载文件从URLTOPATH(字符串url,字符串路径)
{
if(url.StartsWith(GOOGLE_-DRIVE_-DOMAIN)| url.StartsWith(GOOGLE_-DRIVE_-DOMAIN2))
从URLTOPATH(url,路径)返回下载的Google驱动文件;
其他的
返回DownloadFileFromURLToPath(url,路径,null);
}
私有静态文件信息从URLTOPATH下载文件(字符串url、字符串路径、WebClient WebClient)
{
尝试
{
如果(webClient==null)
{
使用(webClient=newWebClient())
{
下载文件(url,路径);
返回新文件信息(路径);
}
}
其他的
{
下载文件(url,路径);
返回新文件信息(路径);
}
}
捕获(WebException)
{
返回null;
}
}
//从Google Drive下载大文件时会出现警告屏幕和
/需要手动确认。考虑该情况并尝试自动确认下载。
//如果出现警告提示
私有静态文件信息下载GoogleDriveFileFromUrlToPath(字符串url,字符串路径)
{
//如果提供的url保证为以下格式,则可以对以下语句进行注释:
// https://drive.google.com/uc?id=FILEID&export=download
url=GetGoogleDriveDownloadLinkFromUrl(url);
使用(CookieAwareWebClient webClient=new CookieAwareWebClient())
{
文件信息下载文件;
//有时驱动器在第一次尝试时返回NID cookie而不是下载警告cookie,
//但在第二次尝试中有效
对于(int i=0;i<2;i++)
{
downloadedFile=DownloadFileFromURLToPath(url、路径、webClient);
如果(downloadedFile==null)
返回null;
//确认页面大约为50KB,不应大于60KB
如果(下载文件.Length>60000)
返回下载的文件;
//下载的文件可能是确认页面,请检查它
字符串内容;
使用(var reader=downloaddedfile.OpenText())
{
//确认页面以开头,前面可以换行
char[]头=新字符[20];
int readCount=reader.ReadBlock(头,0,20);
if(readCount<20 | |!(新字符串(标题)。包含(“”))
返回下载的文件;
content=reader.ReadToEnd();
}
int linkIndex=content.LastIndexOf(“href=\”/uc?”);
如果(链接索引<0)
返回下载的文件;
链接索引+=6;
int linkEnd=content.IndexOf(“”,linkIndex);
我
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( "&", "&" );
}
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;
}
}