简体   繁体   English

WebClient 抛出“WebClient 请求期间发生异常”

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

I know that question has been ask a lot in the inte.net, but yet i didn't found a satisfying answer.我知道这个问题在 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;
        }

I don't have a clue why this throw to me an exception, I tried to download other files, http and https, it doesn't seams to have any difference to the outcome.我不知道为什么这会抛出异常,我尝试下载其他文件 http 和 https,结果似乎没有任何差异。

The given exception:给定的异常:

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

Remove this whole construct:删除整个结构:

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

and replace it by并将其替换为

webClient.DownloadFile(sqlLocalDBUrl, downloadPath);

Then you will still get an error, but at least you will be able to see what is wrong and where.然后你仍然会得到一个错误,但至少你将能够看到哪里出了问题。 The exception and maybe the inner exception will tell you in no uncertain terms what is wrong.异常,也许是内部异常会毫不含糊地告诉你哪里出了问题。 The stack trace will tell you where it went wrong.堆栈跟踪会告诉您哪里出错了。

As it is, you have added this block and all it does is it removes the information you need to find out what is wrong.事实上,您已经添加了这个块,它所做的只是删除了您需要的信息以找出问题所在。 We cannot tell you what went wrong specifically, because your code intentionally removes it.我们无法告诉您具体出了什么问题,因为您的代码有意将其删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WebClient请求期间发生异常 - Exception occurred during a WebClient request Windows服务WebClient请求期间发生异常 - Windows Service An exception occurred during a WebClient request WebClient请求期间发生异常。 在Windows Phone中 - An exception occurred during a WebClient request. in windows phone 使用任务计划程序时“WebClient 请求期间发生异常” - "An exception occurred during a WebClient request" when using the Task Scheduler 使用webclient下载大文件时出现“ WebClient请求期间发生异常” - Getting “An exception occurred during a WebClient request” while download the large file using webclient 为什么从网站下载图像时出现异常:WebClient请求期间发生异常? - Why when downloading images from a website im getting exception: An exception occurred during a WebClient request? 使用C#ASP.NET时,“WebClient请求期间发生异常” - 'An exception occurred during a WebClient request" while using C# ASP.NET 如何重试收到超时异常的Web客户端请求 - How to retry a webclient request that gets timeout exception DispatcherTimer 和 WebClient.DownloadStringAsync 抛出“WebClient 不支持并发 I/O 操作”异常 - DispatcherTimer and WebClient.DownloadStringAsync throw “WebClient does not support concurrent I/O operations” exception WebClient引发异常远程服务器返回错误:(507)存储空间不足 - WebClient throw exception The remote server returned an error: (507) Insufficient Storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM