C#下载报错:An exception occurred during a WebClient request.,不知道如何处理。

            //如果下载失败会请求3次
            string exep = "";
            do
            {
                try
                {
                    if (!Directory.Exists(Dir))
                    {
                        Directory.CreateDirectory(Dir);
                    }
                    using (var wc = new WebClient())
                    {
                        wc.Credentials = CredentialCache.DefaultCredentials;
                        wc.Headers.Add(HttpRequestHeader.UserAgent, "anything");
                        wc.DownloadFile(strFullName, Dir + strFullName.Substring(strFullName.LastIndexOf('/') + 1));
                    }
                    return true;
                }
                catch (System.Net.WebException ex)
                {
                    //图片服务器上也存在也会报错
                    if (ex.Message == "An exception occurred during a WebClient request.")
                    {
                        exep = "occurred";
                    }
                    else {
                        exep = "";
                    }
                }
                catch (Exception ex)
                {
                    LogMsg.WriteLogToApplicationFolderByMonth(ex.ToString() + "-------------------------------------FileURL:" + strFullName);
                    return false;
                }
            } while (exep == "occurred");
            return true;

图片在服务器存在,能够正常访问,但是下载的时候时不时会报“An exception occurred during a WebClient request.”

不知道如何处理,所以就做了一个循环,最多下砸3次。(目前可以解决下载的问题,因为这个问题是偶尔发生在某一个下载请求里,所以用了个循环请求直至3次请求下载完成)

 

转载于:https://www.cnblogs.com/hougelou/p/4223254.html

你可能感兴趣的:(C#下载报错:An exception occurred during a WebClient request.,不知道如何处理。)