/// <inheritdoc/> protected override void Execute() { // Try once without credentials, then retry with if required bool useCredentials = false; while (true) { var request = BuildRequest(useCredentials); try { Download(request); return; } catch (WebException ex) { switch (ex.Status) { case WebExceptionStatus.RequestCanceled: throw new OperationCanceledException(); case WebExceptionStatus.ProtocolError: if (ex.Response is HttpWebResponse response && response.StatusCode == HttpStatusCode.Unauthorized && CredentialProvider != null) { if (useCredentials) { CredentialProvider.ReportInvalid(ex.Response.ResponseUri); } // Retry (but only once when non-interactive) if (CredentialProvider.Interactive || !useCredentials) { useCredentials = true; continue; } } break; } // Wrap exception to add context throw new WebException(string.Format(Resources.FailedToDownload, Source), ex, ex.Status, ex.Response); } } }