简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM