繁体   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