public static string GetDocText(this string url, int?Timeout) { string html = string.Empty; try { using (ConfigurableWebClient client = new ConfigurableWebClient()) { /* Set timeout for webclient */ client.Timeout = Timeout; /* Build url */ Uri innUri = null; //if (!url.StartsWith("https://")) // url = "https://" + url; //if (url.StartsWith("http://")) // url = "http://" + url; Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out innUri); try { client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR " + "3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.2; AskTbFXTV5/5.15.4.23821; BRI/2)"); client.Headers.Add("Vary", "Accept-Encoding"); client.Encoding = Encoding.UTF8; html = client.DownloadString(innUri); if (html.Contains("Pagina non disponibile")) { string str = "site blocked"; str = ""; } if (string.IsNullOrEmpty(html)) { return(string.Empty); } else { return(html); } } catch (Exception ex) { return(""); } finally { client.Dispose(); } } } catch (Exception ex) { return(""); } }
protected virtual IResult DownloadFileAsync(FileInfoWrapper info) { try { using (var client = new ConfigurableWebClient { KeepAlive = false }) { var targetPath = PathUtilities.DownloadFolder; if (!Directory.Exists(targetPath)) return new DirectoryNotFound { Target = targetPath }; var uri = GetDownloadUri(info); if (uri == null) return new CouldNotDownloadFile { Issue = "Could not determine host target", Target = info.FileName }; client.Credentials = GetDownloadCredentials(uri); client.DownloadFileCompleted += DownloadCompletedHandler; client.DownloadProgressChanged += DownloadProgressHandler; _completed = false; if (string.IsNullOrWhiteSpace(info.FileName)) return new CouldNotDownloadFile { Issue = "Could not determine local target", Target = info.FileName }; if (string.IsNullOrWhiteSpace(DisplayName)) DisplayName = info.FileName; targetPath = Path.Combine(targetPath, info.FileName); var resultWrapper = new DownloadInfoWrapper { DisplayName = DisplayName, Result = new NextResult(), StartTime = DateTime.UtcNow }; client.Headers.Add(HttpRequestHeader.UserAgent, string.Format("{0} {1}", Application.Current.GetProductName(), Application.Current.GetVersion())); InterveneIfConnectionLost(); client.DownloadFileAsync(uri, targetPath, resultWrapper); do { Thread.Sleep(5); Application.Current.DoEvents(); // check for cancel _cancelRequested = CancelRequested(); if (!_cancelRequested) continue; // handle cancel stuff client.CancelAsync(); _cancelRequested = true; SetCancelMessages(); } while (_completed == false); UserInterfaceUtilities.WaitForMilliseconds(DateTime.UtcNow, 250); UpdateProgressPercent(100); UpdateTimeEstimate(""); UpdateProgressText(resultWrapper.Result.IsOk() ? "Download complete!" : "Download failed!"); return resultWrapper.Result; } } catch (Exception e) { return new ExceptionOccurred(e); } }