[英]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.