簡體   English   中英

WebClient 拋出“WebClient 請求期間發生異常”

[英]WebClient throw 'An exception occurred during WebClient request'

我知道這個問題在 inte.net 上問了很多,但我還沒有找到滿意的答案。

private string LocalSqlDriverDownloader()
        {
            ProgBar prograssBar = new();
            string sqlLocalDBUrl = "https://download.microsoft.com/download/7/c/1/7c14e92e-bdcb-4f89-b7cf-93543e7112d1/SqlLocalDB.msi";
            string fileName = "SqlLocalDB.msi";
            string directory = $@"{Path.GetPathRoot(Environment.SystemDirectory)}Download"; // C:\Download
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            using WebClient webClient = new();
            webClient.DownloadProgressChanged += (s, e) =>
            {
                Application.Current.Dispatcher?.Invoke(() =>
                {
                    (prograssBar.DataContext as PrograssbarWindowViewModel).PrograssBarValue = e.ProgressPercentage;
                });
            };
            webClient.DownloadFileCompleted += (s, e) =>
            {
                prograssBar.Close();
            };
            string downloadPath = $@"{directory}\{fileName}";
            try
            {
                webClient.DownloadFile(sqlLocalDBUrl, downloadPath);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            prograssBar.ShowDialog();
            return directory;
        }

我不知道為什么這會拋出異常,我嘗試下載其他文件 http 和 https,結果似乎沒有任何差異。

給定的異常:

System.Exception
  HResult=0x80131500
  Message=An exception occurred during WebClient request.
  Source=PulserTesterMultipleHeads
  StackTrace:
   at PulserTesterMultipleHeads.Models.MainTestPageMV.LocalSqlDriverDownloader() in C:\Project\Models\MainTestPageMV.cs:line 955
   at PulserTesterMultipleHeads.Models.MainTestPageMV.LocalSQLDriverInstaller() in C:\Project\Models\MainTestPageMV.cs:line 905
   at PulserTesterMultipleHeads.Models.MainTestPageMV..ctor(Action closeAction, String catalogDesc) in C:\Project\Models\MainTestPageMV.cs:line 70
   at PulserTesterMultipleHeads.UserControls.MainTestPage..ctor() in C:\Project\UserControls\MainTestPage.xaml.cs:line 31

刪除整個結構:

try
{
    webClient.DownloadFile(sqlLocalDBUrl, downloadPath);
}
catch (Exception e)
{
    throw new Exception(e.Message);
}

並將其替換為

webClient.DownloadFile(sqlLocalDBUrl, downloadPath);

然后你仍然會得到一個錯誤,但至少你將能夠看到哪里出了問題。 異常,也許是內部異常會毫不含糊地告訴你哪里出了問題。 堆棧跟蹤會告訴您哪里出錯了。

事實上,您已經添加了這個塊,它所做的只是刪除了您需要的信息以找出問題所在。 我們無法告訴您具體出了什么問題,因為您的代碼有意將其刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM