This code:
try { _wcl.DownloadFile(url, currentFileName); } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null) if ((ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound) Console.WriteLine("\r{0} not found. ", currentFileName); }
downloads the file and reports if a 404 error has occurred.
I decided to upload files asynchronously:
try { _wcl.DownloadFileAsync(new Uri(url), currentFileName); } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null) if ((ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound) Console.WriteLine("\r{0} not found. ", currentFileName); }
Now this blocking block does not work if the server returns a 404 error and WebClient creates an empty file.
source share