いつもお世話になっております。
現在c# にて8.1ギガのZipファイルをダウンロードしてみようと、以下のソースのような処理を書いて試しています。
現在発生している問題点は、ファイルが5.3ギガバイトを超えた時点で、「WebClientに例外的なエラーが発生しました」と
表示されて、完全なダウンロードが出来ない状態となっています。
4回ほど試しましたが、同じ5.3ギガバイト付近(正確には5381421398、5381319801、5381715394、5381385284)で
ダウンロードが中断されてしまいます。
timeoutが問題かと思い、20分に伸ばしてみましたが、5.3ギガバイトダウンロードした時点で、
それ以上ダウンロードされず、タイムアウトの時間を超えてから「WebClientに例外的なエラーが発生しました」と
メッセージが出るようになっています。
現状のソースコードでどこを変更することにより、8.1ギガのZIPファイルをダウンロードすることが
可能となるでしょうか?
また今利用しているWebClient自体、使用することをお勧めされていないようで
「非同期で、ダウンロードの進捗が目に見える、大容量ファイルのダウンロードも出来る」
の条件を満たした方法があるのでしょうか?
よろしくお願いいたします。
using System; using System.ComponentModel; using System.Windows.Forms; using System.Net; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //ダウンロード中のフラグ private bool onDownloading = false; //ダウンロード用のWebClient private MyWebClient wc = null; //処理時間計測用 System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); //ダウンロードの進歩イベント private void wc_DownloadProgressChanged(Object sender, DownloadProgressChangedEventArgs e) { progressBar1.Value = e.ProgressPercentage; } //完了時のイベント private void wc_DownloadFileCompleted(Object sender, AsyncCompletedEventArgs e) { if ((e.Error != null) && (!e.Cancelled)) { // 計測停止 sw.Stop(); TimeSpan ts = sw.Elapsed; MessageBox.Show("処理時間" + ts.ToString() + "ダウンロード中にエラーが発生しました。内容:" + e.Error.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (e.Cancelled) { // 計測停止 sw.Stop(); TimeSpan ts = sw.Elapsed; MessageBox.Show("処理時間" + ts.ToString() + "ダウンロードがキャンセルされました。", "完了", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { // 計測停止 sw.Stop(); TimeSpan ts = sw.Elapsed; MessageBox.Show("処理時間" + ts.ToString() + "データのダウンロードが完了しました。", "完了", MessageBoxButtons.OK, MessageBoxIcon.Information); } onDownloading = false; } //ダウンロードボタンクリック private void button1_Click(object sender, EventArgs e) { // 計測開始 sw.Start(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //8G.zipファイルをダウンロードする Uri uri = new Uri("8g.zip"); if (wc == null) { wc = new MyWebClient(); //webClientに進行状況、完了時のイベントハンドラを追加 wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged); wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted); } //ダウンロード開始 wc.DownloadFileAsync(uri, "output.zip"); onDownloading = true; } //キャンセルボタンクリック private void button2_Click(object sender, EventArgs e) { //ダウンロード中なら、ダウンロードをキャンセルする if (onDownloading) { if (wc != null) { wc.CancelAsync(); } } } } //タイムアウト時間を延ばすためクラス作成 public class MyWebClient : System.Net.WebClient { public int Timeout { get; set; } = 120 * 1000; protected override WebRequest GetWebRequest(Uri address) { var w = base.GetWebRequest(address); w.Timeout = Timeout; return w; } } }
回答1件
あなたの回答
tips
プレビュー